summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoss Barnowski <rossbar@berkeley.edu>2021-02-08 13:22:20 -0800
committerGitHub <noreply@github.com>2021-02-08 16:22:20 -0500
commit0efdf69fc1ed83547737f66d5e50d1b718c36404 (patch)
tree486f795565333e5d302f703c242ccdc0c50fa269
parentd39d1dcfe3f4af765950ca5c39ba8f22ae200511 (diff)
downloadnetworkx-0efdf69fc1ed83547737f66d5e50d1b718c36404.tar.gz
Use Pillow for viewing AGraph output and deprecate default_opener (#4600)
* Replace utils.default_opener with Image.show(). * Deprecate nx.utils.misc.default_opener. * No PIL on pypy.
-rw-r--r--doc/developer/deprecations.rst1
-rw-r--r--networkx/conftest.py3
-rw-r--r--networkx/drawing/nx_agraph.py12
-rw-r--r--networkx/drawing/tests/test_agraph.py9
-rw-r--r--networkx/utils/misc.py11
5 files changed, 23 insertions, 13 deletions
diff --git a/doc/developer/deprecations.rst b/doc/developer/deprecations.rst
index 7da9fe28..c3e67c3d 100644
--- a/doc/developer/deprecations.rst
+++ b/doc/developer/deprecations.rst
@@ -52,6 +52,7 @@ Version 3.0
* In ``utils/misc.py`` remove ``iterable``.
* In ``utils/misc.py`` remove ``is_list_of_ints``.
* In ``utils/misc.py`` remove ``consume``.
+* In ``utils/misc.py`` remove ``default_opener``.
* Remove ``utils/contextmanagers.py`` and related tests.
* In ``drawing/nx_agraph.py`` remove ``display_pygraphviz`` and related tests.
* In ``algorithms/chordal.py`` replace ``chordal_graph_cliques`` with ``_chordal_graph_cliques``.
diff --git a/networkx/conftest.py b/networkx/conftest.py
index 5c7dbaa9..27725d40 100644
--- a/networkx/conftest.py
+++ b/networkx/conftest.py
@@ -41,6 +41,9 @@ def set_warnings():
"ignore", category=DeprecationWarning, message="is_string_like is deprecated"
)
warnings.filterwarnings(
+ "ignore", category=DeprecationWarning, message="default_opener is deprecated"
+ )
+ warnings.filterwarnings(
"ignore", category=DeprecationWarning, message="make_str is deprecated"
)
warnings.filterwarnings(
diff --git a/networkx/drawing/nx_agraph.py b/networkx/drawing/nx_agraph.py
index 87da1af5..ad2a79ea 100644
--- a/networkx/drawing/nx_agraph.py
+++ b/networkx/drawing/nx_agraph.py
@@ -324,7 +324,7 @@ def view_pygraphviz(
The filename used to save the image. If None, save to a temporary
file. File formats are the same as those from pygraphviz.agraph.draw.
show : bool, default = True
- Whether to display the graph with `networkx.utils.default_opener`,
+ Whether to display the graph with :mod:`PIL.Image.show`,
default is `True`. If `False`, the rendered graph is still available
at `path`.
@@ -434,7 +434,9 @@ def view_pygraphviz(
# Show graph in a new window (depends on platform configuration)
if show:
- nx.utils.default_opener(path.name)
+ from PIL import Image
+
+ Image.open(path.name).show()
return path.name, A
@@ -463,12 +465,14 @@ def display_pygraphviz(graph, path, format=None, prog=None, args=""):
calls if you experience problems.
"""
+ from PIL import Image
import warnings
warnings.warn(
"display_pygraphviz is deprecated and will be removed in NetworkX 3.0. "
"To view a graph G using pygraphviz, use nx.nx_agraph.view_pygraphviz(G). "
- "To view a graph from file, consider nx.utils.default_opener(filename).",
+ "To view a graph from file, consider an image processing libary like "
+ "`Pillow`, e.g. ``PIL.Image.open(path.name).show()``",
DeprecationWarning,
)
if format is None:
@@ -482,4 +486,4 @@ def display_pygraphviz(graph, path, format=None, prog=None, args=""):
# We must close the file before viewing it.
graph.draw(path, format, prog, args)
path.close()
- nx.utils.default_opener(filename)
+ Image.open(filename).show()
diff --git a/networkx/drawing/tests/test_agraph.py b/networkx/drawing/tests/test_agraph.py
index 0cb10de1..45971ae7 100644
--- a/networkx/drawing/tests/test_agraph.py
+++ b/networkx/drawing/tests/test_agraph.py
@@ -239,12 +239,3 @@ class TestAGraph:
pos = list(pos.values())
assert len(pos) == 5
assert len(pos[0]) == 3
-
- def test_display_pygraphviz_deprecation_warning(self):
- G = nx.complete_graph(2)
- path_name, A = nx.nx_agraph.view_pygraphviz(G, show=False)
- # Monkeypatch default_opener to prevent window opening
- nx.utils.default_opener = lambda x: None
- with pytest.warns(DeprecationWarning, match="display_pygraphviz is deprecated"):
- with open(path_name, "wb") as fh:
- nx.nx_agraph.display_pygraphviz(A, fh, prog="dot")
diff --git a/networkx/utils/misc.py b/networkx/utils/misc.py
index c1390e42..56b1d182 100644
--- a/networkx/utils/misc.py
+++ b/networkx/utils/misc.py
@@ -158,12 +158,23 @@ def generate_unique_node():
def default_opener(filename):
"""Opens `filename` using system's default program.
+ .. deprecated:: 2.6
+ default_opener is deprecated and will be removed in version 3.0.
+ Consider an image processing library to open images, such as Pillow::
+
+ from PIL import Image
+ Image.open(filename).show()
+
Parameters
----------
filename : str
The path of the file to be opened.
"""
+ warnings.warn(
+ "default_opener is deprecated and will be removed in version 3.0. ",
+ DeprecationWarning,
+ )
from subprocess import call
cmds = {