-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusage.py
80 lines (68 loc) · 1.72 KB
/
usage.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import dash_vega
from dash import Dash, callback, html, Input, Output,dcc
import altair as alt
from vega_datasets import data
import json
source = data.cars()
styles = {
'pre': {
'border': 'thin lightgrey solid',
'overflowX': 'scroll'
}
}
app = Dash(__name__)
app.layout = html.Div([
dcc.Dropdown(
id = "input",
options = [3,4,5,6],
value = 4
),
dash_vega.Vega(id='vega'),
html.Div([
dcc.Markdown("""
**Click Data**
Click on points in the graph.
"""),
html.Pre(id='click-data', style=styles['pre']),
]),
html.Div([
dcc.Markdown("""
**hover Data**
Click on points in the graph.
"""),
html.Pre(id='hover-data', style=styles['pre']),
]),
])
@callback(
Output('vega', 'spec'),
Input('input', 'value')
)
def update_vega(value):
df = source[source['Cylinders'] == value]
chart = (
alt.Chart(df).mark_circle().encode(
x='Horsepower',
y='Miles_per_Gallon',
size='Acceleration',
color='Origin',
tooltip=['Name', 'Origin', 'Horsepower', 'Miles_per_Gallon']
)
.interactive()
.properties(title="Altair / Vega-Lite",width="container",height=400)
.to_dict()
)
return chart
@callback(
Output("click-data","children"),
Input("vega","clickData")
)
def click_data(clickData):
return json.dumps(clickData, indent=2)
@callback(
Output("hover-data","children"),
Input("vega","hoverData")
)
def hover_data(hoverData):
return json.dumps(hoverData, indent=2)
if __name__ == '__main__':
app.run_server(debug=True)