Forums

Dash app - Very slow to start plotly?

Hi, I have some plotly charts in my dash app and recently I have frequently found a long pause on the line of my code where the first chart is requested. On my local machine, each chart is made instantly (circa 0 seconds), and indeed this is sometimes the case on my hosted pythonanywhere version, but often I find the first chart takes circa 7 seconds to load and sometimes as many as 25 seconds. This appears to be only for the first chart, and then others are loaded instantly (even if I swap the chart contents around), so I presume the problem is with loading plotly in itself.

Example simple chart code:

import plotly.express as px

fig 1 =px.line(histdf,x='date',y='score')

I have tried updating to the latest version of plotly, and also tried building the charts as Plotly Graph Objects instead of Plotly Express objects but I get similar behaviour. This is quite annoying as this is now by far the slowest part of all my code. Is there anything else I can try?

Many thanks!

What do you see in the browser's dev tools? What is slow?

I'm using a simple print statement before and after the first call to a plotly chart and seeing on the python anywhere log files that this is usually taking several (or more) seconds, although occasionally it will do it in 0 seconds. All subsequent calls to any other plotly charts always take 0 seconds. On my local machine, all calls to a plotly chart (including the first call) take 0 seconds.

Just to be clear I am observing this a few seconds or minutes after successfully running the app, so I don't think it is to do with the shutdown of an app that hasn't been used for a long time.

Is the print statement in the Python code, so that you can check the output in the site's server log? If so, have you added the "flush=True" keyword arg to the end? I'm wondering if buffering of output from Python might be affecting your metrics (even though it sounds like this is a real issue regardless of the logging output).

Hi, yes the print statements are in the code, before and after each suspected problem line. I wasn't aware of the flush keyword so that will have been False by default, I will insert it as True and see if it helps !

Great! Let us know if you find anything interesting.

Hi, I found this issue to be less common in recent weeks but it seems to be back again today, this is the same with flush=True added to my print statements as discussed. Is there anything on your side that could be causing the slow loading or running of the Plotly package in particular? Many thanks, Andy

Hi Andy -- there's nothing on our side that comes to mind. From the "print" statements, do you see which parts of your code are running more slowly than usual?

Hi Giles, I had a couple of days without noticing the problem (and I hoped my upgrade of Dash to the latest version had cured it!) but unfortunately I am having the problem again today.

To be clear, here is an illustration of my code and print statements:

starttime = time.time()
print(f'Program start ',flush=True)
lotsofdataprocessing()
print(f'Processing time {time.time() - starttime}',flush=True)

starttime = time.time()
fig1 = px.line(plotly chart args)
fig1.update_layout(update layout args)
print(f'fig1 time {time.time() - starttime}',flush=True)

starttime = time.time()
fig2 = px.line(second plotly chart args)
fig2.update_layout(update layout args)
print(f'fig2 time {time.time() - starttime}',flush=True)

In the logs I am then usually see something like (note I have added illustrative PythonAnywhere log timestamps in addition to my print outputs)

12:00:00 Program start time
12:00:02  Processing time 2 seconds
12:00:02  fig1 time 0.1 seconds
12:00:02  fig2 time 0.1 seconds

When I have the problem, I instead see something like this:

12:00:00 Program start time
12:00:02  Processing time 2 seconds
12:00:21  fig1 time 19 seconds
12:00:21  fig2 time 0.1 seconds

This 19 second delay is also visible in the browser. As mentioned, interestingly it doesn't seem to matter if I swap fig1 and fig2 around - whichever fig is called first experiences the massive delay, and the second fig is loaded in 0.1 seconds or less, which as discussed is what made me wonder if it is to do with the Plotly package being slowly loaded or installed somehow. I have never experienced this issue running the code on my local machine.

Thanks for any further ideas you can provide !

Where are you getting the data that goes into the charts? Is getting the data included in the time that you''re measuring? What if you measure the getting of the data separately to the charting?

Hi Glenn, I am measuring the fetching and processing of data in the "processing" lines above, and the time counted in my charting is literally just the 2 charting lines:

fig1 = px.line(plotly chart args)
fig1.update_layout(update layout args)

I will keep looking into this and may reach out for support on the Plotly/Dash forums and will certainly post back here if I find out anything that sheds more light on this. Until then I can use static/simple matplotlib or seaborn charts which hopefully don't have this poblem.

In the meantime, I have hit a much more pressing problem with my app and my understanding of scalability, I will write a new topic about this and hope that someone can point me in the right direction! (Edit - here https://eu.pythonanywhere.com/forums/topic/348/)