-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathshiny_express_all_new_style.py
147 lines (120 loc) · 3.99 KB
/
shiny_express_all_new_style.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
from random import randrange
import pandas as pd
from pytabulator import TableOptions, Tabulator, TabulatorContext, render_tabulator
from pytabulator.utils import create_columns
from pytabulator.formatters import ProgressFormatter, TickCrossFormatter
from pytabulator.editors import ListEditor, InputEditor, ProgressEditor
from shiny import reactive, render
from shiny.express import input, ui
# Fetch data
#
df = pd.read_csv(
"https://raw.githubusercontent.com/datasciencedojo/datasets/master/titanic.csv"
)[["PassengerId", "Name", "Pclass", "Sex", "Age", "Fare", "Survived"]]
# Setup
#
table_options = TableOptions(
columns=create_columns(
df,
default_filter=True,
default_editor=True,
updates={
"Pclass": {
"formatter": "star",
"formatterParams": {"stars": 3},
"hozAlign": "center",
},
# "Survived": {"formatter": "tickCross"},
# "Fare": {"formatter": "progress", "hozAlign": "left"},
},
),
height=413,
pagination=True,
pagination_add_row="table",
layout="fitColumns",
index="PassengerId",
add_row_pos="top",
selectable_rows=True,
history=True,
)
# Shiny Express App
#
with ui.div(style="padding-top: 0px;"):
ui.input_action_button("trigger_download", "Download")
ui.input_action_button("add_row", "Add row")
ui.input_action_button("delete_selected_rows", "Delete selected rows")
ui.input_action_button("undo", "Undo")
ui.input_action_button("redo", "Redo")
ui.input_action_button("trigger_get_data", "Submit data")
ui.div(
ui.input_text("name", "Click on 'Add row' to add the Person to the table."),
style="padding-top: 20px;",
)
ui.div("Click on a row to print the name of the person.", style="padding: 10px;"),
@render.code
async def txt():
print(input.tabulator_row_clicked())
return input.tabulator_row_clicked()["Name"]
ui.div(
"Select multiple rows to print the names of the selected persons.",
style="padding: 10px;",
),
@render.code
def selected_rows():
data = input.tabulator_rows_selected()
output = [item["Name"] for item in data]
return "\n".join(output)
@render_tabulator
def tabulator():
return (
Tabulator(df, table_options)
.set_options(editTriggerEvent="dblclick")
.set_column_formatter("Fare", ProgressFormatter(), hoz_align="left")
.set_column_formatter("Survived", TickCrossFormatter(), hoz_align="center")
.set_column_editor("Sex", ListEditor())
.set_column_editor("Name", InputEditor())
.set_column_editor("Fare", ProgressEditor(), hoz_align="left")
)
@reactive.Effect
@reactive.event(input.trigger_download)
async def trigger_download():
print("download triggered")
async with TabulatorContext("tabulator") as table:
table.trigger_download("csv")
@reactive.Effect
@reactive.event(input.add_row)
async def add_row():
async with TabulatorContext("tabulator") as table:
table.add_row(
{
"Name": input.name() or "Hans",
"Age": randrange(55),
"Survived": randrange(2),
"PassengerId": randrange(10000, 20000, 1),
"SibSp": randrange(9),
}
)
@reactive.Effect
@reactive.event(input.delete_selected_rows)
async def delete_selected_rows():
async with TabulatorContext("tabulator") as table:
table.delete_selected_rows()
@reactive.Effect
@reactive.event(input.undo)
async def undo():
async with TabulatorContext("tabulator") as table:
table.undo()
@reactive.Effect
@reactive.event(input.redo)
async def redo():
async with TabulatorContext("tabulator") as table:
table.redo()
@reactive.Effect
@reactive.event(input.trigger_get_data)
async def trigger_get_data():
async with TabulatorContext("tabulator") as table:
table.trigger_get_data()
@reactive.Effect
@reactive.event(input.tabulator_data)
def tabulator_data():
print(input.tabulator_data())