Skip to content

Commit 7f6bd04

Browse files
committed
🔥 add streamlit integration example
1 parent f25cc4e commit 7f6bd04

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

examples/README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,19 @@ Additionally, this notebook highlights how to use the `FigureWidget` its on-clic
1717

1818
## 1. Dash apps
1919

20-
The [dash_apps](dash_apps/dash_app.py) folder contains example dash apps in
20+
The [dash_apps](dash_apps/) folder contains example dash apps in
2121
which `plotly-resampler` is integrated
2222

2323
| app-name | description |
2424
| --- | --- |
2525
| [file visualization](dash_apps/dash_app.py) | load and visualize multiple `.parquet` files with plotly-resampler |
2626
| [dynamic sine generator](dash_apps/construct_dynamic_figures.py) | expeonential sine generator which uses [pattern matching callbacks](https://dash.plotly.com/pattern-matching-callbacks) to remove and construct plotly-resampler graphs dynamically |
27-
| [dynamic static graph](dash_apps/dash_app_coarse_fine.py) | Visualization dashboard in which a dynamic (i.e., plotly-resampler graph) and static graph (i.e., go.Figure) are shown (made for [this issue](https://github.com/predict-idlab/plotly-resampler/issues/56)). Relayout events on the coarse graph update the dynamic graph.
27+
| [dynamic static graph](dash_apps/dash_app_coarse_fine.py) | Visualization dashboard in which a dynamic (i.e., plotly-resampler graph) and static graph (i.e., go.Figure) are shown (made for [this issue](https://github.com/predict-idlab/plotly-resampler/issues/56)). Relayout events on the coarse graph update the dynamic graph.
28+
29+
## 2. Other apps
30+
31+
The [other_apps](other_apps/) folder contains examples of `plotly-resampler` being *integrated* in other apps / frameworks
32+
33+
| app-name | description |
34+
| --- | --- |
35+
| [streamlit integration](other_apps/streamlit_app.py) | visualize a large noisy sine in a [streamlit](https://streamlit.io/) app |
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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

Comments
 (0)