plotly.py icon indicating copy to clipboard operation
plotly.py copied to clipboard

If IPython is merely installed, don't assume that it's actually used

Open pfalcon opened this issue 7 months ago • 1 comments

In my virtual environment, there happens to be installed IPython package. It wasn't installed by myself, it's merely a transitive dependency of some another package. It's neither intended to be used, nor is actually used by my project. However, mere presense of the IPython package, due to the over-simplistic decision code in this module (io._renderers) leads to drastic behavior change in Plotly.py: calling Figure.repr_html() causes lots of JS garbage to be printed on terminal (by common sense, this should be a side-effects free function, only returning a value). On a higher level, this breaks natural Plotly.py integration in frameworks like Shiny.py (because again, instead of HTML representation being returned for a framework to process, parts of that representation are dumped on terminal).

The real underlying issue is that Plotly.py should not force-import any "optional" packages behind users' back. However, this is a deeper issue requiring consideration and will be reported separately. This patch is a quick solution for the problem describe above.

From interactive session:

>>> import IPython
>>> IPython.get_ipython().__class__.__name__
'NoneType'

Code PR

  • [x] I have read through the contributing notes and understand the structure of the package. In particular, if my PR modifies code of plotly.graph_objects, my modifications concern the codegen files and not generated files.
  • [ ] I have added tests (if submitting a new feature or correcting a bug) or modified existing tests.
  • [ ] For a new feature, I have added documentation examples in an existing or new tutorial notebook (please see the doc checklist as well).
  • [ ] I have added a CHANGELOG entry if fixing/changing/adding anything substantial.
  • [ ] For a new feature or a change in behaviour, I have updated the relevant docstrings in the code to describe the feature or behaviour (please see the doc checklist as well).

pfalcon avatar Jun 07 '25 21:06 pfalcon

Example of how this affects downstream projects: https://github.com/posit-dev/py-shinywidgets/issues/201 . I'm an experienced Python user, and it took me half a day to debug this issue. I imagine how frustrating it may be for novice users who just wanted to try one of those well-popularized modern Python data visualization packages.

pfalcon avatar Jun 07 '25 21:06 pfalcon

Hi @pfalcon , I've just merged a fix in https://github.com/plotly/plotly.py/pull/5258 which takes a slightly different approach to this same issue. Thank you for the contribution and please let me know if that fix addresses the issue on your end!

emilykl avatar Oct 01 '25 22:10 emilykl