-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathreference_app.py
171 lines (147 loc) · 4.87 KB
/
reference_app.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
from io import StringIO
from pathlib import Path
import pandas as pd
import panel as pn
from panel_gwalker import GraphicWalker
pn.extension("filedropper", sizing_mode="stretch_width", notifications=True)
ROOT = Path(__file__).parent
PANEL_GW_URL = "https://github.com/panel-extensions/panel-graphic-walker"
GW_LOGO = "https://kanaries.net/_next/static/media/kanaries-logo.0a9eb041.png"
GW_API = "https://github.com/Kanaries/graphic-walker"
GW_GUIDE_URL = "https://docs.kanaries.net/graphic-walker/data-viz/create-data-viz"
SPEC_CAPACITY_STATE = "https://cdn.jsdelivr.net/gh/panel-extensions/panel-graphic-walker@main/examples/reference_app/spec_simple.json"
SPEC_SIMPLE = "https://cdn.jsdelivr.net/gh/panel-extensions/panel-graphic-walker@main/examples/reference_app/spec_capacity_state.json"
ACCENT = "#5B8FF9"
def _label(value):
return pn.pane.Markdown(value, margin=(-20, 5))
def _section_header(value):
return pn.pane.Markdown(value, margin=(-5, 5))
@pn.cache
def get_data():
return pd.read_parquet(
"https://datasets.holoviz.org/windturbines/v1/windturbines.parq"
)
def get_example_download():
df = pd.DataFrame(
{"country": ["Denmark", "Germany"], "population": [5_000_000, 80_000_000]}
)
sio = StringIO()
df.to_csv(sio, index=False)
sio.seek(0)
return sio
button_style = dict(button_type="primary", button_style="outline")
walker = GraphicWalker(
get_data(),
spec=SPEC_CAPACITY_STATE,
sizing_mode="stretch_both",
kernel_computation=True,
)
core_settings = pn.Column(
walker.param.kernel_computation,
walker.param.spec,
walker.param.config,
walker.param.renderer,
pn.widgets.IntInput.from_param(
walker.param.page_size, visible=walker.is_enabled("page_size")
),
pn.widgets.Checkbox.from_param(
walker.param.hide_profiling, visible=walker.is_enabled("hide_profiling")
),
pn.widgets.IntInput.from_param(
walker.param.index, visible=walker.is_enabled("index")
),
pn.widgets.RadioButtonGroup.from_param(
walker.param.tab,
visible=walker.is_enabled("tab"),
button_type="primary",
button_style="outline",
),
pn.widgets.TextInput.from_param(
walker.param.container_height, visible=walker.is_enabled("container_height")
),
name="Core",
)
style_settings = pn.Column(
_label("Appearance"),
pn.widgets.RadioButtonGroup.from_param(walker.param.appearance, **button_style),
_label("Theme Key"),
pn.widgets.RadioButtonGroup.from_param(walker.param.theme_key, **button_style),
name="Style",
)
file_upload = pn.widgets.FileDropper(
accepted_filetypes=["text/csv"],
multiple=False,
max_file_size="5MB",
styles={"border": "1px dashed black", "border-radius": "4px"},
height=85,
)
file_download = pn.widgets.FileDownload(
callback=get_example_download, filename="example.csv"
)
export_controls = walker.export_controls()
exported = pn.rx("""
```bash
{value}
```
""").format(value=export_controls.param.value)
export_section = pn.Column(export_controls, exported, name="Export")
save_section = pn.Column(walker.save_controls(), name="Save")
docs_section = f"## Docs\n\n- [panel-graphic-walker]({PANEL_GW_URL})\n- [Graphic Walker Usage Guide]({GW_GUIDE_URL})\n- [Graphic Walker API]({GW_API})"
def _apply_spec(value):
if walker.spec == value:
walker.param.trigger("spec")
else:
walker.spec = value
simple_spec = pn.widgets.Button(
name="Simple",
button_type="primary",
button_style="outline",
on_click=lambda event: _apply_spec(SPEC_SIMPLE),
)
initial_spec = pn.widgets.Button(
name="Initial",
button_type="primary",
button_style="outline",
on_click=lambda event: _apply_spec(SPEC_CAPACITY_STATE),
)
no_spec = pn.widgets.Button(
name="No Spec",
button_type="primary",
button_style="outline",
on_click=lambda event: _apply_spec(None),
)
@pn.depends(file_upload, watch=True)
def _update_walker(value):
if value:
text = next(iter(value.values()))
df = pd.read_csv(StringIO(text))
if not df.empty:
walker.object = df
# Can be removed once https://github.com/panel-extensions/panel-graphic-walker/issues/33 is resolved
pn.state.notifications.success(
"New dataset uploaded. Add a new chart to use it.", duration=5000
)
pn.template.FastListTemplate(
logo=GW_LOGO,
title="Panel Graphic Walker Reference App",
sidebar=[
"## Data Input",
file_upload,
file_download,
"## Spec Input",
pn.Row(simple_spec, no_spec, initial_spec),
"## Settings",
pn.Accordion(
core_settings,
style_settings,
export_section,
save_section,
width=320,
active=[0],
),
docs_section,
],
main=[walker],
main_layout=None,
accent="#5B8FF9",
).servable()