Graphs should render as graphics by default in notebooks
What is the feature or improvement you would like to see?
In a notebook environment, when evaluating a graph, it now displays something like <igraph.Graph at 0x11fd18400>. Instead, it should plot the graph. It should show a graphical representation. This should happen for small graphs only (it's useless and counterproductive for large graphs which are slow to lay out or don't render nicely at on-screen sizes).
This would be a major improvement to user experience, and is in the spirit of making igraph accessible to everyone, and making full use of interactive notebook features.
Both Mathematica and SageMath do this by default in their respective notebook environments. Use them as a model for how to implement this.
You can try SageMath at cocalc.com. SageMath does not display the result when it is assigned to a variable, but it does otherwise. First evaluate g=Graph([(0,1),(1,2),(2,0)]), then g. You can try Mathematica at https://www.wolframcloud.com/ (free account needed). Evaluate RandomGraph[{10,20}].
Use cases for the feature Usability.
References See Mathematica and SageMath.
I once made this work for networkx, I should dig it up. igraph should do it by default whenever run in a Jupyter notebook.
I think what's missing here is a default _repr_svg_() method for Graph objects. I wonder what to do with large graphs, though -- shall we render a placeholder or shall we somehow bail out of _repr_svg_() and show the usual Python repr(...) representation instead?
I am quite in favour of not giving graphs an automatic plotting instead of printing useful info on number of nodes and edges, directness, and attributes.
If this becomes default people will rely on it for their plots and complain whenever they need adjustments. Also, it will reinforce the impression - already too popular - that a graph and its 2D layout are the same thing.
ig.plot(g) is short enough don't you think?
As someone who has been using notebooks for 20 years, to me it seems a no-brainer that a graphical representation is the way to go in notebook environments, and that this increases productivity during interactive work. It is telling that it is precisely SageMath (which had a notebook interface well before Jupyter—so it's developers understand the concept well) does this by default.
Don't think of this as "automatic plotting". That's not what it is, and it is not a replacement for plotting. In a notebook, you enter an input, evaluate it, and look at the output. What if the output is a graph? I want to "see" that graph (or get information about it) right away. I don't want to type extra commands for it (Python is way too verbose as it is). The question is: what is the most informative way to show it? For small graphs, that is clearly a graphical representation.
As for showing information about the number of vertices/edges, directedness, and other O(1)-computable information: absoutely. This is useful to add on to the graphical representation (just like Sage does), and show alone for large graphs where a graphical representation does not make sense.
In summary, in order to make good use of a notebook interface, all kinds of output objects should have useful and informative display forms. Then the question is: what is the most useful display form of a graph? The same question can be asked for other common objects that igraph works with: clusterings, palettes, etc.
Here are some more ideas (or sprouts of ideas, as I don't yet have a complete picture in my head):
I am wondering if it would make sense for igraph to have some persistent configuration options that control how it behaves in a notebook.
To be clear: In most cases, I think that it is not appropriate for an analysis library to have persistent (persisting across sessions) config options that control what types of results it produces. How it computes should be the same in all environments—anything else would be confusing.
But how the computation results are displayed in a notebook are something that a user may want to configure. I am thinking of things such as:
- Do we want a graphical or textual representation? (In case some people really don't want the graphical one for a reason I don't see now.)
- What is the graph size limit above which the representation switches from graphical to textual?
- What is the default size of the graphical output? Note that this should refer to the graphical representation of graphs only, and not explicit plotting. A script that creates publication figures should produce the same result (in the same size) in all environments.
- Should there be a progress bar / status messages by default?
- ... and so on ...
I am also wondering if these settings could be made in some other ways than using a persistent storage. What about the "magic commands" of IPython / Jupyter? Could we use those? I am not very familiar with them.
I am wondering if it would make sense for igraph to have some persistent configuration options that control how it behaves in a notebook.
The Python interface already has a configuration framework, vaguely based on how Matpliotlib's configuration looked like >10 years ago when I "borrowed" the concept. The settings are stored in a file named .igraphrc hidden somewhere in the user's home directory.
In some sense, this is a horrible idea if you think about python-igraph as a library because an external file on the user's system may completely change how the library behaves, and this is not something that you want from a library as a software developer. On the other hand, if you think about python-igraph as an interactive tool for the exploration and analysis of graphs, it makes sense to give the user an opportunity to tune the behaviour of the tool to his/her needs. So I totally agree with @szhorvat here. Whether to show the graph itself in PNG/SVG format or a summary of O(1) things about the graph in textual format is a matter of taste, and as such it should be configurable when python-igraph is used as a tool (and not as a library).
I think that this again seems to indicate that there should be a way for python-igraph to figure out whether it's running in a notebook environment, and if so (and only if so), it should parse its configuration file and tune its behaviour to the user's taste. However, at that point I start to wonder whether configuration files are usable in online environments like Google Colab, or whether there's an other mechanism (specific to IPython / Jupyter, which all these online notebook environments seem to use) that is better for storing configuration options.
Just found out that the default configuration has a key named ipython.inlining.Plot with a boolean value that controls whether instances of the Plot class are inlined as SVG files in IPython / Jupyter or not :) I don't know whether it still works or not (haven't touched this part of the library in ages), but that's a start.
Highlighting this as a clear TODO item:
However, at that point I start to wonder whether configuration files are usable in online environments like Google Colab, or whether there's an other mechanism (specific to IPython / Jupyter, which all these online notebook environments seem to use) that is better for storing configuration options.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 14 days if no further activity occurs. Thank you for your contributions.