Py.Cafe

fomightez/

streamlit_and_plotly_together_example

Streamlit app with Plotly Exampe

DocsPricing
  • app.py
  • requirements.txt
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# Based on Serge de Gosson de Varennes' excellent code at https://stackoverflow.com/a/78590766/8508004 for streamlit where says "Here is the same thing if you are not using jupyter notebook:"
# Only other change aside from replaceing the Streamlit template code with the code from SO, was adding `plotly` to `requirements.txt` 
import streamlit as st
import numpy as np
import pandas as pd
import plotly.graph_objects as go
import plotly.express as px

Z = st.sidebar.slider("Number of Ensembles", min_value=10, max_value=50, value=20, step=10)
frames = st.sidebar.slider("Number of Frames", min_value=10, max_value=100, value=20, step=10)

N = 50  
x = np.linspace(0, 2 * np.pi, 40)
y = np.array([np.sin(x + phase*Z) for phase in np.linspace(0, 2 * np.pi, N)])

df = pd.DataFrame({
    'x': np.tile(x, N * frames),
    'y': np.sin(np.tile(x, N * frames) + np.repeat(np.linspace(0, 2 * np.pi, frames), N * 40)),
    'line_id': np.repeat(np.arange(N), 40 * frames),
    'frame_id': np.repeat(np.arange(frames), N * 40)
})

fig = px.line(df, x='x', y='y', animation_frame='frame_id', animation_group='line_id', 
              line_group='line_id', color='line_id')

fig.update_layout(
    title="Animated Line Plot",
    xaxis_title="X Axis",
    yaxis_title="Y Axis",
    showlegend=False
)

st.plotly_chart(fig)