-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Stefan Kuethe
committed
Sep 24, 2024
1 parent
8e54aed
commit e826953
Showing
3 changed files
with
155 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
143 changes: 143 additions & 0 deletions
143
docs/examples/getting_started/shiny_express_all_new_style.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
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 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") | ||
) | ||
|
||
|
||
@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()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters