✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.
🐇 Announcing Dash VTK for 3d simulation graphics. Check out the March webinar.

Unable to incorporate plotly.express into a Mac App created using py2app and tkinter

I’m creating a Mac App using tkinter and py2app and am encountering problems when trying to incorporate plotly.express. (Python 3.7 on Mac OS 10.15.4)

The app asks the user for the time series length and overlays the plots of three random time series using that specified length along with the labels, ‘a’, ‘b’, and ‘c’.

import numpy as np
import matplotlib.pyplot as plt
from tkinter import Tk, Label, Button, Entry,
import matplotlib.pyplot as plt
import plotly.express as px
import pandas as pd

window = Tk()
plt.switch_backend(‘TkAgg’)

window.title(“Plotter”)
window.geometry(‘500x500’)

length_label=Label(window, text=“Length”)
length_label.grid(row=0,column=0)

length = Entry(window,width=20)
length.grid(row=0,column=1)

‘’‘Plot time series’’’

def _plot():
np.random.seed(123)
num_times=int(length.get())
X = np.random.randn(num_times,3)
df=pd.DataFrame(X, columns=[‘a’,‘b’,‘c’])
df[‘x’] = df.index
df_melt = pd.melt(df, id_vars=“x”, value_vars=df.columns[:-1])
fig=px.line(df_melt, x=“x”, y=“value”,color=“variable”)
fig.show()

plot_button = Button(master=window, text=“Plot”, command=_plot)
plot_button.grid(row=1, column=0)

‘’‘Quit Button’’’
def _quit():
window.quit()
window.destroy()

quit_button = Button(master=window, text=“Quit”, command=_quit)
quit_button.grid(row=2, column=0)

window.mainloop()

I’m able to create the app using py2app and run it from the terminal. The app opens when double clicking but hangs when I attempt to plot. Replacing the lines

df['x'] = df.index
df_melt = pd.melt(df, id_vars="x", value_vars=df.columns[:-1])
fig=px.line(df_melt, x="x", y="value",color="variable")
fig.show() 

with

plt.plot(df)
plt.show()

results in something that works absolutely fine, which suggests the problem is with plotly.express