Skip to content

Commit 9526a40

Browse files
Mike ProsserMike Prosser
authored andcommitted
added simple graph example
1 parent d3a1df8 commit 9526a40

File tree

5 files changed

+381
-80
lines changed

5 files changed

+381
-80
lines changed

examples/simple_graph/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Simple Graph Example
2+
3+
This example demonstrates using nipanel with Streamlit to display a dynamic sine wave using the `streamlit-echarts` library.
4+
5+
## Features
6+
7+
- Generates sine wave data with varying frequency
8+
- Displays the data in an interactive chart using ECharts
9+
- Updates automatically every 1 second
10+
- Shows statistics about the displayed data
11+
12+
### Required Software
13+
14+
- Python 3.9 or later
15+
16+
### Usage
17+
18+
Run `poetry run examples/simple_graph/simple_graph.py`
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"""Example of using nipanel to display a sine wave graph using st_echarts."""
2+
3+
import math
4+
import time
5+
from pathlib import Path
6+
7+
import numpy as np
8+
9+
import nipanel
10+
11+
12+
panel_script_path = Path(__file__).with_name("simple_graph_panel.py")
13+
panel = nipanel.create_panel(panel_script_path)
14+
15+
amplitude = 1.0
16+
frequency = 1.0
17+
num_points = 100
18+
19+
try:
20+
print(f"Panel URL: {panel.panel_url}")
21+
print("Press Ctrl+C to exit")
22+
23+
# Generate and update the sine wave data periodically
24+
while True:
25+
time_points = np.linspace(0, num_points, num_points)
26+
sine_values = amplitude * np.sin(frequency * time_points)
27+
28+
panel.set_value("time_points", time_points.tolist())
29+
panel.set_value("sine_values", sine_values.tolist())
30+
panel.set_value("amplitude", amplitude)
31+
panel.set_value("frequency", frequency)
32+
33+
# Slowly vary the frequency for a more dynamic visualization
34+
frequency = 1.0 + 0.5 * math.sin(time.time() / 5.0)
35+
time.sleep(1)
36+
37+
except KeyboardInterrupt:
38+
print("Exiting...")
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
"""Example of displaying a sine wave using streamlit-echarts in a nipanel."""
2+
3+
import streamlit as st
4+
from streamlit_echarts import st_echarts
5+
6+
from nipanel import initialize_panel
7+
8+
9+
st.set_page_config(page_title="Simple Graph Example", page_icon="📈", layout="wide")
10+
st.title("Simple Sine Wave Visualization")
11+
12+
panel = initialize_panel()
13+
time_points = panel.get_value("time_points", [0.0])
14+
sine_values = panel.get_value("sine_values", [0.0])
15+
amplitude = panel.get_value("amplitude", 1.0)
16+
frequency = panel.get_value("frequency", 1.0)
17+
18+
col1, col2, col3, col4, col5 = st.columns(5)
19+
with col1:
20+
st.metric("Amplitude", f"{amplitude:.2f}")
21+
with col2:
22+
st.metric("Frequency", f"{frequency:.2f} Hz")
23+
with col3:
24+
st.metric("Min Value", f"{min(sine_values):.3f}")
25+
with col4:
26+
st.metric("Max Value", f"{max(sine_values):.3f}")
27+
with col5:
28+
st.metric("Data Points", len(sine_values))
29+
30+
# Prepare data for echarts
31+
data = [{"value": [x, y]} for x, y in zip(time_points, sine_values)]
32+
33+
# Configure the chart options
34+
options = {
35+
"animation": False, # Disable animation for smoother updates
36+
"title": {"text": "Sine Wave"},
37+
"tooltip": {"trigger": "axis"},
38+
"xAxis": {"type": "value", "name": "Time (s)", "nameLocation": "middle", "nameGap": 30},
39+
"yAxis": {
40+
"type": "value",
41+
"name": "Amplitude",
42+
"nameLocation": "middle",
43+
"nameGap": 30,
44+
},
45+
"series": [
46+
{
47+
"data": data,
48+
"type": "line",
49+
"showSymbol": True,
50+
"smooth": True,
51+
"lineStyle": {"width": 2, "color": "#1f77b4"},
52+
"areaStyle": {"color": "#1f77b4", "opacity": 0.3},
53+
"name": "Sine Wave",
54+
}
55+
],
56+
}
57+
58+
# Display the chart
59+
st_echarts(options=options, height="400px")

0 commit comments

Comments
 (0)