Black Lives Matter. Please consider donating to Black Girls Code today.
Dash Enterprise delivers an incredible 21x cost savings 💸Download the e-book!

Plotly Super Slow and Sometimes Hanging Up on display

Hello All,

Another beginner questions, I am using Plotly to plot some data from CSVs, they are relatively large files, however my problem seems to be file size agnostic because even when I cut the file size in half I still run into this issue.

The issue is Plotly Hangs up alot as it tries to plot the values its being given
Often this will show up as the browser window just hanging, and then timing out saying the site cant be reached
Is it the lightweight web-server that plotly spins up? I am unsure
So sometimes Plotly will be incredibly fast and plot values no problem. Other times it just hangs up in the browser waiting for a response and crashes. I have no clue as to why. My code is listed below (feel free to give me any tips very new to Python and Plotly)

import csv
import numpy as np
import sys
import re
import pandas as pd
import plotly as ex
import plotly.express as px
import plotly.graph_objects as go

file_name = sys.argv[1]
df = pd.read_csv(file_name)

col_mapping = [c[1] for c in enumerate(df.columns)] ##This is to get the column names of the CSV to build the title

##BUILD THE TITLE
i = len(col_mapping)
q = 0;

titleString = ’ ’
for variableName in col_mapping:
if((i-1) == q):
titleString += ‘%s’ % (variableName)
else:
titleString += '%s vs ’ % (variableName)
q = q + 1

titleString += ’ ’

if i == 3:
my_df= df.iloc[: , [0, 1, 2]].copy() ##This is so I dont have to know the column names in the CSV beforehand

fig = go.Figure()

# Add traces
fig.add_trace(go.Scatter(x=df.iloc[:,0], y=df.iloc[:,1],
                    mode='markers',
                    name='markers'))
fig.add_trace(go.Scatter(x=df.iloc[:,0], y=df.iloc[:,2],
                    mode='markers',
                    name='markers'))
fig.update_layout(title=titleString)
fig.show()
       
fig = go.Figure(data=[go.Table(
    header=dict(values=list(df.columns),fill_color='paleturquoise',align='left'),
    cells=dict(values=[df.iloc[:,0], df.iloc[:,1], df.iloc[:,2]],fill_color='lavender',align='left'))
])
fig.update_layout(title=titleString)
fig.show()

elif i < 3:
my_df= df.iloc[: , [0, 1]].copy()
z = len(my_df)
#This profuces a table and always pop up fast
fig = go.Figure(data=[go.Table(
header=dict(values=list(df.columns),fill_color=‘paleturquoise’,align=‘left’),
cells=dict(values=[df.iloc[:,0], df.iloc[:,1]],fill_color=‘lavender’,align=‘left’))
])
fig.update_layout(title=titleString)
fig.show()
#This is where things hang up usaully
fig1 = px.line(my_df, x=my_df.columns[0], y=my_df.columns[1], title = titleString)
fig1.show()

else:
print(“You are trying to plot to many values, you need to choose 3 at most”)

So instead of using fig.show I ended up using fig.write_html(titleString + ‘plot.html’, auto_open=True) and it seems to work pretty fast?