Skip to content

Commit db19821

Browse files
adejumoridwanwch
andauthored
More realistic file import example (#582)
Co-authored-by: Winston Chang <[email protected]>
1 parent b231425 commit db19821

File tree

2 files changed

+93
-17
lines changed

2 files changed

+93
-17
lines changed
Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,54 @@
11
import pandas as pd
22

3-
from shiny import App, Inputs, Outputs, Session, render, ui
3+
from shiny import App, Inputs, Outputs, Session, reactive, render, ui
44
from shiny.types import FileInfo
55

66
app_ui = ui.page_fluid(
7-
ui.layout_sidebar(
8-
ui.panel_sidebar(
9-
ui.input_file("file1", "Choose CSV File", accept=[".csv"], multiple=False),
10-
ui.input_checkbox("header", "Header", True),
11-
width=5,
12-
),
13-
ui.panel_main(
14-
ui.output_ui("contents"),
15-
),
7+
ui.input_file("file1", "Choose CSV File", accept=[".csv"], multiple=False),
8+
ui.input_checkbox_group(
9+
"stats",
10+
"Summary Stats",
11+
choices=["Row Count", "Column Count", "Column Names"],
12+
selected=["Row Count", "Column Count", "Column Names"],
1613
),
14+
ui.output_table("summary"),
1715
)
1816

1917

2018
def server(input: Inputs, output: Outputs, session: Session):
19+
@reactive.Calc
20+
def parsed_file():
21+
file: list[FileInfo] = input.file1()
22+
if file is None:
23+
return pd.DataFrame()
24+
return pd.read_csv(file[0]["datapath"])
25+
2126
@output
22-
@render.ui
23-
def contents():
24-
if input.file1() is None:
25-
return "Please upload a csv file"
26-
f: list[FileInfo] = input.file1()
27-
df = pd.read_csv(f[0]["datapath"], header=0 if input.header() else None)
28-
return ui.HTML(df.to_html(classes="table table-striped"))
27+
@render.table
28+
def summary():
29+
df = parsed_file()
30+
31+
if df.empty:
32+
return pd.DataFrame()
33+
34+
# Get the row count, column count, and column names of the DataFrame
35+
row_count = df.shape[0]
36+
column_count = df.shape[1]
37+
names = df.columns.tolist()
38+
column_names = ", ".join(str(name) for name in names)
39+
40+
# Create a new DataFrame to display the information
41+
info_df = pd.DataFrame(
42+
{
43+
"Row Count": [row_count],
44+
"Column Count": [column_count],
45+
"Column Names": [column_names],
46+
}
47+
)
48+
49+
# input.stats() is a list of strings; subset the columns based on the selected
50+
# checkboxes
51+
return info_df.loc[:, input.stats()]
2952

3053

3154
app = App(app_ui, server)

shiny/examples/input_file/app.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import pandas as pd
2+
3+
from shiny import App, reactive, render, ui
4+
5+
app_ui = ui.page_fluid(
6+
ui.input_file("file1", "Upload a CSV file", accept=".csv"),
7+
ui.input_checkbox_group(
8+
"stats",
9+
"Summary Stats",
10+
choices=["Row Count", "Column Count", "Column Names"],
11+
selected=["Row Count", "Column Count", "Column Names"],
12+
),
13+
ui.output_table("summary"),
14+
)
15+
16+
17+
def server(input: Inputs, output: Outputs, session: Session):
18+
@reactive.Calc
19+
def parsed_file():
20+
file: list[FileInfo] = input.file1()
21+
if file is None:
22+
return pd.DataFrame()
23+
return pd.read_csv(file[0]["datapath"])
24+
25+
@output
26+
@render.table
27+
def summary():
28+
df = parsed_file()
29+
30+
if df.empty:
31+
return pd.DataFrame()
32+
33+
# Get the row count, column count, and column names of the DataFrame
34+
row_count = df.shape[0]
35+
column_count = df.shape[1]
36+
names = df.columns.tolist()
37+
column_names = ", ".join(str(name) for name in names)
38+
39+
# Create a new DataFrame to display the information
40+
info_df = pd.DataFrame(
41+
{
42+
"Row Count": [row_count],
43+
"Column Count": [column_count],
44+
"Column Names": [column_names],
45+
}
46+
)
47+
48+
# input.stats() is a list of strings; subset the columns based on the selected
49+
# checkboxes
50+
return info_df.loc[:, input.stats()]
51+
52+
53+
app = App(app_ui, server)

0 commit comments

Comments
 (0)