Skip to content

Commit 0e0591e

Browse files
committed
Added Dash and Python demo
1 parent 55c1b31 commit 0e0591e

File tree

3 files changed

+172
-0
lines changed

3 files changed

+172
-0
lines changed

demo/dash/dash_app_with_tooltip.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
"""
2+
This Dash application is intended to be run with a custom version of plotly.js that includes tooltip functionality.
3+
The custom plotly.js file should be placed in the assets folder of the Dash project.
4+
"""
5+
import dash
6+
from dash import dcc, html
7+
import plotly.graph_objs as go
8+
import datetime
9+
import numpy as np
10+
11+
# Initialize the Dash app
12+
app = dash.Dash(__name__)
13+
14+
# Generate date and time values
15+
def generate_date_time(start, count):
16+
dates = []
17+
current = datetime.datetime.fromisoformat(start)
18+
for _ in range(count):
19+
dates.append(current)
20+
current += datetime.timedelta(hours=7)
21+
return dates
22+
23+
# Generate random y-values
24+
def generate_random_y_values(count):
25+
return np.random.random(count) * 20
26+
27+
# Generate data points
28+
# Define the base trace dictionary
29+
base_trace = {
30+
'name': None,
31+
'x': None,
32+
'y': None,
33+
'type': 'scatter',
34+
'mode': 'markers',
35+
'marker': {'size': 10},
36+
'tooltiptemplate': "%{fullData.name}<br>%{x|%H:%M:%S}<br>y: %{y:.2e}",
37+
'tooltip': {
38+
'align': 'left',
39+
'arrowcolor': 'blue',
40+
'arrowhead': 2,
41+
'arrowsize': 1.2,
42+
'arrowwidth': 1,
43+
'font': {
44+
'color': 'red',
45+
'family': 'Arial',
46+
'size': 16
47+
},
48+
'showarrow': True,
49+
'xanchor': 'left'
50+
}
51+
}
52+
53+
# Create 3 traces using dictionary comprehension
54+
traces = [
55+
{
56+
**base_trace,
57+
'name': f'Trace {i+1}',
58+
'x': generate_date_time(f'2025-04-0{i+1}T12:00:00', 5),
59+
'y': generate_random_y_values(5)
60+
}
61+
for i in range(3)
62+
]
63+
64+
# Layout definition
65+
layout = go.Layout(
66+
title='Custom Tooltip Example',
67+
hovermode='closest'
68+
)
69+
70+
# App layout
71+
app.layout = html.Div([
72+
html.H1("Plotly Tooltip Button Test"),
73+
dcc.Graph(
74+
id='plot',
75+
figure={
76+
'data': [*traces],
77+
'layout': layout
78+
},
79+
config={
80+
'editable': True,
81+
'modeBarButtonsToAdd': ['tooltip', 'hoverclosest', 'hovercompare', 'togglespikelines'],
82+
'displaylogo': False,
83+
}
84+
)
85+
])
86+
87+
if __name__ == '__main__':
88+
app.run_server(debug=True)
File renamed without changes.

demo/python/plotly-py.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import plotly.graph_objs as go
2+
import plotly.io as pio
3+
import datetime
4+
import numpy as np
5+
6+
custom_output_file = 'plot.html'
7+
plotly_js_path = '../../dist/plotly.js'
8+
9+
10+
def generate_date_time(start, count):
11+
dates = []
12+
current = datetime.datetime.fromisoformat(start)
13+
for _ in range(count):
14+
dates.append(current)
15+
current += datetime.timedelta(hours=7)
16+
return dates
17+
18+
19+
def generate_random_y_values(count):
20+
return np.random.random(count) * 20
21+
22+
23+
# Define the base trace dictionary
24+
base_trace = {
25+
'name': None,
26+
'x': None,
27+
'y': None,
28+
'type': 'scatter',
29+
'mode': 'markers',
30+
'marker': {'size': 10},
31+
'tooltiptemplate': "%{fullData.name}<br>%{x|%H:%M:%S}<br>y: %{y:.2e}",
32+
'tooltip': {
33+
'align': 'left',
34+
'arrowcolor': 'blue',
35+
'arrowhead': 2,
36+
'arrowsize': 1.2,
37+
'arrowwidth': 1,
38+
'font': {
39+
'color': 'red',
40+
'family': 'Arial',
41+
'size': 16
42+
},
43+
'showarrow': True,
44+
'xanchor': 'left'
45+
}
46+
}
47+
48+
# Create 3 traces using dictionary comprehension
49+
traces = [
50+
go.Scatter(
51+
name=f'Trace {i+1}',
52+
x=generate_date_time(f'2025-04-0{i+1}T12:00:00', 5),
53+
y=generate_random_y_values(5),
54+
mode='markers',
55+
marker={'size': 10},
56+
# not supported yet. additional properties are blocked
57+
# by plotly\basedatatypes.py", line 4392, in _process_kwargs
58+
# tooltiptemplate=base_trace['tooltiptemplate'],
59+
# tooltip=base_trace['tooltip']
60+
)
61+
for i in range(3)
62+
]
63+
64+
# Define the layout for the plot
65+
layout = go.Layout(
66+
title='Custom Tooltip Example',
67+
hovermode='closest'
68+
)
69+
70+
# Create the figure
71+
fig = go.Figure(data=[trace.to_plotly_json() for trace in traces], layout=layout)
72+
73+
# Define the configuration options
74+
config = {
75+
'editable': True,
76+
'modeBarButtonsToAdd': ['tooltip', 'hoverclosest', 'hovercompare', 'togglespikelines'],
77+
'displaylogo': False,
78+
'displayModeBar': True
79+
}
80+
81+
# Save the figure to an HTML file with custom plotly.js path
82+
fig.write_html(file=custom_output_file, include_plotlyjs=plotly_js_path, config=config, validate=True, auto_open=True)
83+
84+
print(f"Custom HTML with Plotly plot saved as {custom_output_file}")

0 commit comments

Comments
 (0)