question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Feature Request] Allow direct import of utils.PlotlyJSONEncoder for faster Dash startup time

See original GitHub issue

This issue maybe belongs more to the related plotly.py repository, but the motivation comes from using Dash. Please feel free to transfer the issue to plotly.py if/when more useful.


Running

time python -c "import dash"

real	0m2.172s
user	0m1.183s
sys	0m0.566s

shows real/wall-clock typically above 2 seconds on my system, even when the whole Python distribution is installed locally on the same computer. When using a shared network disk Python distribution, it of course gets slower. 🕙

Running (with Python 3.7 or higher)

python -X importtime -c "import dash"

shows that a large portion of the time is spent on unused plotly.graph_objs imports. The import stems from this line in Dash https://github.com/plotly/dash/blob/8ee358826cd4318a3edc52fbf71e0bdda2369984/dash/dash.py#L23 where dash/dash.py is using PlotlyJSONEncoder from plotly.utils.

As a quick hack, changing that line to

from _plotly_utils.utils import PlotlyJSONEncoder

reduces the import dash wall time to 0.6-0.7 seconds, i.e. around 70% reduction in import time for dash. 🏎 Starting a Dash app suddenly felt much more instant (which is nice during development).

The plotly Python package I guess is free to change the “private package” _plotly_utils without releasing a major release, so the “hack” above is not a permanent nice solution for dash (even though dash does not do any pinning of plotly version today, so a new major release of plotly might already break previous dash releases, but that is a separate issue 🙂).

I guess the best solution might be to change plotly/__init__.py to not import all subpackages, such that the consumer of the plotly package can choose what to import. It is common to have to import subpackages explicitly, e.g.

python -c "import matplotlib; print(matplotlib.pyplot.__file__)"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
AttributeError: module 'matplotlib' has no attribute 'pyplot'

would not work, while

python -c "import matplotlib.pyplot; print(matplotlib.pyplot.__file__)"
[...]/python3.7/site-packages/matplotlib/pyplot.py

does. You also typically in the plotly.py documentation see lines like

import plotly.graph_objects as go

which is an example of explicit subpackage import, and would work even if plotly/__init__.py does not import graph_objects into its namespace.

Related issue: https://github.com/plotly/plotly.py/issues/740

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:10 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
jonmmeasecommented, Apr 10, 2020

Startup time will be greatly improved by https://github.com/plotly/plotly.py/pull/2368.

Thanks to @anders-kiaer for pointing out the potential of PEP 562. Baking this into our code generation class hierarchy really helps.

1reaction
anders-kiaercommented, Apr 18, 2020

🎉 I would consider this issue as solved now after #2368 and lazy imports (if someone uses 🐍 Python < 3.7 and wants the speedup, they should just update their Python minor version… Python 3.7 release was back in 2018, and Python 3.6 has EOL/last security fix next year).

Thanks for implementing PEP562 @jonmmease! Looking forward to test it out in Dash 🚀

Read more comments on GitHub >

github_iconTop Results From Across the Web

[Feature Request] Allow direct import of utils ... - GitHub
where dash/dash.py is using PlotlyJSONEncoder from plotly.utils . ... reduces the import dash wall time to 0.6-0.7 seconds, i.e. around 70% ...
Read more >
How to integrate plotly.express chart to flask app?
I'll cut out most of the data work and figure building to just show what you need to do. Imports needed. You'll need...
Read more >
Web Visualization with Plotly and Flask. - Towards Data Science
But first let's do a quick comparison of a simple Dash app and it's ... import plotly.express as pxapp = Flask(__name__)@app.route('/')
Read more >
Latest Feature Requests topics - Anytype Community
The place to post, contribute to, & vote for feature requests! Development is prioritized by what's most desired within community.
Read more >
plotly Changelog - PyUp.io
`px.timeline()` now allows `hover_data` formatting of start and end times ... `plotly.utils`, in order to transform NumPy arrays to data b64 URIs (which...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found