xflr6 / graphviz

Simple Python interface for Graphviz
https://graphviz.readthedocs.io
MIT License
1.59k stars 209 forks source link

Add _repr_html_ method #200

Closed oscargus closed 1 year ago

oscargus commented 1 year ago

Closes #199

This adds a _repr_html_ method to Graph and Digraph. This is useful for displaying graphs directly in e.g. sphinx-gallery. The method is simply the SVG generation as SVG is valid HTML.

I think that Jupyter first looks at _repr_mimebundle_, so it shouldn't affect that.

codecov-commenter commented 1 year ago

Codecov Report

Merging #200 (20fe86b) into master (126cbce) will not change coverage. The diff coverage is 100.00%.

:exclamation: Your organization is not using the GitHub App Integration. As a result you may experience degraded service beginning May 15th. Please install the Github App Integration for your organization. Read more.

@@            Coverage Diff            @@
##            master      #200   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           33        33           
  Lines         1089      1090    +1     
=========================================
+ Hits          1089      1090    +1     
Impacted Files Coverage Δ
graphviz/jupyter_integration.py 100.00% <100.00%> (ø)

:mega: We’re building smart automated test selection to slash your CI/CD build times. Learn more

xflr6 commented 1 year ago

Thanks for the PR!

I think that Jupyter first looks at _reprmimebundle, so it shouldn't affect that.

Can you try to render https://github.com/xflr6/graphviz/blob/master/examples/graphviz-notebook.ipynb with this branch?

AFAICT this change makes all graphs image being included twice (once as image/svg+xml, once as text/html with the same content). Assuming that this is not intended.

oscargus commented 1 year ago

If it renders twice, it is actually the fault of Jupyter as an object can have any number of _repr_*_ methods: https://ipython.readthedocs.io/en/stable/config/integrating.html#custom-methods

But as mentioned in #199, it may be better to solve this on the sphinx-gallery side (although there should be no harm in providing this as well, modulo that it is up to the "terminal" to determine which representation to render).

xflr6 commented 1 year ago

Thanks for checking, closing: superseded by https://github.com/sphinx-gallery/sphinx-gallery/pull/1138