Repository: PrefectHQ/prefect

Issue:
Problem visualizing a Flow
When I call flow.visualize(), there is no graph created. Debugging this I found that the problem was the following code in flow.py, from line 1238
       if filename:
            graph.render(filename, view=False, format=format)
        else:
            try:
                from IPython import get_ipython
                
                assert get_ipython().config.get("IPKernelApp") is not None              <----------------- the problem
            except Exception:

Now in order to see if the value was really None I added this line of code:
        if filename:
            graph.render(filename, view=False, format=format)
        else:
            try:
                from IPython import get_ipython
                test = get_ipython().config.get("IPKernelApp")   <-----------------------line added
                assert get_ipython().config.get("IPKernelApp") is not None
            except Exception:

and it worked. I know can visualize the graph. Without the line  it does not work.
I have no idea why this worked.


Discussion:
Hi @rpitre, were you running inside ipython or a jupyter notebook at the time? Can you provide steps to reproduce your error?
No, not running inside ipython or jupyter note book. I am just creating a flow and calling flow.visualize(). I have created complex flow and flow.run() always works without problem. But flow.visialize() never works without my added line code.
Try specifying a filename 
`flow.visualize(filename="/var/tmp/flow1")`

Resolving patch:
diff --git a/src/prefect/core/flow.py b/src/prefect/core/flow.py
--- a/src/prefect/core/flow.py
+++ b/src/prefect/core/flow.py
@@ -1383,8 +1383,11 @@ def get_color(task: Task, map_index: int = None) -> str:
             try:
                 from IPython import get_ipython
 
-                assert get_ipython().config.get("IPKernelApp") is not None
+                in_ipython = get_ipython().config.get("IPKernelApp") is not None
             except Exception:
+                in_ipython = False
+
+            if not in_ipython:
                 with tempfile.NamedTemporaryFile(delete=False) as tmp:
                     tmp.close()
                     try:


Test patch:
diff --git a/tests/core/test_flow.py b/tests/core/test_flow.py
--- a/tests/core/test_flow.py
+++ b/tests/core/test_flow.py
@@ -1232,7 +1232,16 @@ def test_skip_validation_in_init_with_kwarg():
     assert Flow(name="test", edges=[e1, e2], validate=False)
 
 
-@pytest.mark.xfail(raises=ImportError, reason="viz extras not installed.")
+try:
+    import graphviz
+
+    graphviz.pipe("dot", "png", b"graph {a -- b}", quiet=True)
+    no_graphviz = False
+except Exception:
+    no_graphviz = True
+
+
+@pytest.mark.skipif(no_graphviz, reason="viz extras not installed.")
 class TestFlowVisualize:
     def test_visualize_raises_informative_importerror_without_python_graphviz(
         self, monkeypatch
@@ -1242,6 +1251,7 @@ def test_visualize_raises_informative_importerror_without_python_graphviz(
 
         with monkeypatch.context() as m:
             m.setattr(sys, "path", "")
+            m.delitem(sys.modules, "graphviz", raising=False)
             with pytest.raises(ImportError, match=r"pip install 'prefect\[viz\]'"):
                 f.visualize()