-
Notifications
You must be signed in to change notification settings - Fork 114
More realistic file import example #582
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
aa5baff
change input_file example
adejumoridwan 55337dc
change input_file example
adejumoridwan ab7857f
More realistic file import example
adejumoridwan 5c9b3c0
Merge branch 'main' of https://github.com/rstudio/py-shiny into changes
adejumoridwan 2e6fb39
add more realistic file import example
adejumoridwan 413eaec
Merge remote-tracking branch 'upstream/main' into changes
adejumoridwan 5974ed0
add more realistic file import example
adejumoridwan 207da86
Merge branch 'main' of https://github.com/rstudio/py-shiny into changes
adejumoridwan 4c30cc1
Merge branch 'main' of https://github.com/rstudio/py-shiny into changes
adejumoridwan bdcb81b
improve file import example
adejumoridwan 5e4af8e
Update shiny/examples/input_file/app.py
adejumoridwan e891bfa
Update shiny/examples/input_file/app.py
adejumoridwan ecca9b3
Update shiny/examples/input_file/app.py
adejumoridwan b520b23
Update shiny/examples/input_file/app.py
adejumoridwan 980af26
Merge branch 'main' of https://github.com/rstudio/py-shiny into changes
adejumoridwan d1f37d2
improve file input example
adejumoridwan ed8b330
Merge branch 'main' of https://github.com/rstudio/py-shiny into changes
adejumoridwan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
@@ -1,31 +1,54 @@ | ||
import pandas as pd | ||
|
||
from shiny import App, Inputs, Outputs, Session, render, ui | ||
from shiny import App, Inputs, Outputs, Session, reactive, render, ui | ||
from shiny.types import FileInfo | ||
|
||
app_ui = ui.page_fluid( | ||
ui.layout_sidebar( | ||
ui.panel_sidebar( | ||
ui.input_file("file1", "Choose CSV File", accept=[".csv"], multiple=False), | ||
ui.input_checkbox("header", "Header", True), | ||
width=5, | ||
), | ||
ui.panel_main( | ||
ui.output_ui("contents"), | ||
), | ||
ui.input_file("file1", "Choose CSV File", accept=[".csv"], multiple=False), | ||
ui.input_checkbox_group( | ||
"stats", | ||
"Summary Stats", | ||
choices=["Row Count", "Column Count", "Column Names"], | ||
selected=["Row Count", "Column Count", "Column Names"], | ||
), | ||
ui.output_table("summary"), | ||
) | ||
|
||
|
||
def server(input: Inputs, output: Outputs, session: Session): | ||
@reactive.Calc | ||
def parsed_file(): | ||
file: list[FileInfo] = input.file1() | ||
if file is None: | ||
return pd.DataFrame() | ||
return pd.read_csv(file[0]["datapath"]) | ||
|
||
@output | ||
@render.ui | ||
def contents(): | ||
if input.file1() is None: | ||
return "Please upload a csv file" | ||
f: list[FileInfo] = input.file1() | ||
df = pd.read_csv(f[0]["datapath"], header=0 if input.header() else None) | ||
return ui.HTML(df.to_html(classes="table table-striped")) | ||
@render.table | ||
def summary(): | ||
df = parsed_file() | ||
|
||
if df.empty: | ||
return pd.DataFrame() | ||
|
||
# Get the row count, column count, and column names of the DataFrame | ||
row_count = df.shape[0] | ||
column_count = df.shape[1] | ||
names = df.columns.tolist() | ||
column_names = ", ".join(str(name) for name in names) | ||
|
||
# Create a new DataFrame to display the information | ||
info_df = pd.DataFrame( | ||
{ | ||
"Row Count": [row_count], | ||
"Column Count": [column_count], | ||
"Column Names": [column_names], | ||
} | ||
) | ||
|
||
# input.stats() is a list of strings; subset the columns based on the selected | ||
# checkboxes | ||
return info_df.loc[:, input.stats()] | ||
|
||
|
||
app = App(app_ui, server) |
This file contains hidden or 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,53 @@ | ||
import pandas as pd | ||
|
||
from shiny import App, reactive, render, ui | ||
|
||
app_ui = ui.page_fluid( | ||
ui.input_file("file1", "Upload a CSV file", accept=".csv"), | ||
ui.input_checkbox_group( | ||
"stats", | ||
"Summary Stats", | ||
choices=["Row Count", "Column Count", "Column Names"], | ||
selected=["Row Count", "Column Count", "Column Names"], | ||
), | ||
ui.output_table("summary"), | ||
) | ||
|
||
|
||
def server(input: Inputs, output: Outputs, session: Session): | ||
@reactive.Calc | ||
def parsed_file(): | ||
file: list[FileInfo] = input.file1() | ||
if file is None: | ||
return pd.DataFrame() | ||
return pd.read_csv(file[0]["datapath"]) | ||
|
||
@output | ||
@render.table | ||
def summary(): | ||
df = parsed_file() | ||
|
||
if df.empty: | ||
return pd.DataFrame() | ||
|
||
# Get the row count, column count, and column names of the DataFrame | ||
row_count = df.shape[0] | ||
column_count = df.shape[1] | ||
names = df.columns.tolist() | ||
column_names = ", ".join(str(name) for name in names) | ||
|
||
# Create a new DataFrame to display the information | ||
info_df = pd.DataFrame( | ||
{ | ||
"Row Count": [row_count], | ||
"Column Count": [column_count], | ||
"Column Names": [column_names], | ||
} | ||
) | ||
|
||
# input.stats() is a list of strings; subset the columns based on the selected | ||
# checkboxes | ||
return info_df.loc[:, input.stats()] | ||
|
||
|
||
app = App(app_ui, server) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.