|
| 1 | +"""Minimal streamlit app example. |
| 2 | +
|
| 3 | +This example shows how to integrate plotly-resampler in a streamlit app. |
| 4 | +The following thee steps are required; |
| 5 | +1. use FigureResampler |
| 6 | +2. run the visualization (which is a dash app) in a (sub)process on a certain port |
| 7 | +3. add as iframe component to streamlit |
| 8 | +
|
| 9 | +To run this example execute the following command: |
| 10 | +$ streamlit run streamlit_app.py |
| 11 | +
|
| 12 | +""" |
| 13 | + |
| 14 | +__author__ = "Jeroen Van Der Donckt" |
| 15 | + |
| 16 | +import plotly.graph_objects as go |
| 17 | +from plotly_resampler import FigureResampler |
| 18 | + |
| 19 | +# 0. Create a noisy sine wave |
| 20 | +import numpy as np |
| 21 | +x = np.arange(1_000_000) |
| 22 | +noisy_sin = (3 + np.sin(x / 200) + np.random.randn(len(x)) / 10) * x / 1_000 |
| 23 | + |
| 24 | +### 1. Use FigureResampler |
| 25 | +fig = FigureResampler(default_n_shown_samples=2_000) |
| 26 | +fig.add_trace(go.Scattergl(name='noisy sine', showlegend=True), hf_x=x, hf_y=noisy_sin) |
| 27 | +fig.update_layout(height=700) |
| 28 | + |
| 29 | +### 2. Run the visualization (which is a dash app) in a (sub)process on a certain port |
| 30 | +# Note: starting a process allows executing code after `.show_dash` is called |
| 31 | +from multiprocessing import Process |
| 32 | +port = 9022 |
| 33 | +proc = Process( |
| 34 | + target=fig.show_dash, kwargs=dict(mode="external", port=port) |
| 35 | +).start() |
| 36 | + |
| 37 | +# Deleting the lines below this and running this file will result in a classic running dash app |
| 38 | +# Note: for just a dash app it is not even necessary to execute .show_dash in a (sub)process |
| 39 | + |
| 40 | +### 3. Add as iframe component to streamlit |
| 41 | +import streamlit.components.v1 as components |
| 42 | +components.iframe(f'http://localhost:{port}', height=700) |
0 commit comments