-
Notifications
You must be signed in to change notification settings - Fork 83
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
Gordon Shotwell
authored
Jan 25, 2024
1 parent
9c5a87c
commit e24d44e
Showing
17 changed files
with
301 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import matplotlib.pyplot as plt | ||
import numpy as np | ||
|
||
from shiny import reactive | ||
from shiny.express import input, render, ui | ||
|
||
ui.input_slider("n", "Number of observations", min=0, max=1000, value=500) | ||
ui.input_action_link("go", "Go!") | ||
|
||
|
||
@render.plot(alt="A histogram") | ||
# reactive.event() to invalidate the plot when the button is pressed but not when | ||
# the slider is changed | ||
@reactive.event(input.go, ignore_none=False) | ||
def plot(): | ||
np.random.seed(19680801) | ||
x = 100 + 15 * np.random.randn(input.n()) | ||
fig, ax = plt.subplots() | ||
ax.hist(x, bins=30, density=True) | ||
return fig |
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,8 @@ | ||
from shiny.express import input, render, ui | ||
|
||
ui.input_checkbox("somevalue", "Some value", False) | ||
|
||
|
||
@render.ui | ||
def value(): | ||
return input.somevalue() |
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,18 @@ | ||
from shiny import req | ||
from shiny.express import input, render, ui | ||
|
||
ui.input_checkbox_group( | ||
"colors", | ||
"Choose color(s):", | ||
{ | ||
"red": ui.span("Red", style="color: #FF0000;"), | ||
"green": ui.span("Green", style="color: #00AA00;"), | ||
"blue": ui.span("Blue", style="color: #0000AA;"), | ||
}, | ||
) | ||
|
||
|
||
@render.ui | ||
def val(): | ||
req(input.colors()) | ||
return "You chose " + ", ".join(input.colors()) |
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,24 @@ | ||
from datetime import date | ||
|
||
from shiny.express import ui | ||
|
||
ui.input_date("date1", "Date:", value="2016-02-29") | ||
# Default value is the date in client's time zone | ||
ui.input_date("date2", "Date:") | ||
# value is always yyyy-mm-dd, even if the display format is different | ||
ui.input_date("date3", "Date:", value="2016-02-29", format="mm/dd/yy") | ||
# Pass in a Date object | ||
ui.input_date("date4", "Date:", value=date(2016, 2, 29)) | ||
# Use different language and different first day of week | ||
ui.input_date("date5", "Date:", language="ru", weekstart=1) | ||
# Start with decade view instead of default month view | ||
ui.input_date("date6", "Date:", startview="decade") | ||
# Disable Mondays and Tuesdays. | ||
ui.input_date("date7", "Date:", daysofweekdisabled=[1, 2]) | ||
# Disable specific dates. | ||
ui.input_date( | ||
"date8", | ||
"Date:", | ||
value="2016-02-29", | ||
datesdisabled=["2016-03-01", "2016-03-02"], | ||
) |
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,27 @@ | ||
from datetime import date | ||
|
||
from shiny.express import ui | ||
|
||
ui.input_date_range("daterange1", "Date range:", start="2001-01-01", end="2010-12-31") | ||
# Default start and end is the current date in the client's time zone | ||
ui.input_date_range("daterange2", "Date range:") | ||
# start and end are always specified in yyyy-mm-dd, even if the display | ||
# format is different | ||
ui.input_date_range( | ||
"daterange3", | ||
"Date range:", | ||
start="2001-01-01", | ||
end="2010-12-31", | ||
min="2001-01-01", | ||
max="2012-12-21", | ||
format="mm/dd/yy", | ||
separator=" - ", | ||
) | ||
# Pass in Date objects | ||
ui.input_date_range( | ||
"daterange4", "Date range:", start=date(2001, 1, 1), end=date(2010, 12, 31) | ||
) | ||
# Use different language and different first day of week | ||
ui.input_date_range("daterange5", "Date range:", language="de", weekstart=1) | ||
# Start with decade view instead of default month view | ||
ui.input_date_range("daterange6", "Date range:", startview="decade") |
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,48 @@ | ||
import pandas as pd | ||
|
||
from shiny import reactive | ||
from shiny.express import input, render, ui | ||
from shiny.types import FileInfo | ||
|
||
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"], | ||
) | ||
|
||
|
||
@reactive.Calc | ||
def parsed_file(): | ||
file: list[FileInfo] | None = input.file1() | ||
if file is None: | ||
return pd.DataFrame() | ||
return pd.read_csv(file[0]["datapath"]) # pyright: ignore[reportUnknownMemberType] | ||
|
||
|
||
@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()] |
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,8 @@ | ||
from shiny.express import input, render, ui | ||
|
||
ui.input_numeric("obs", "Observations:", 10, min=1, max=100) | ||
|
||
|
||
@render.code | ||
def value(): | ||
return input.obs() |
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,11 @@ | ||
from shiny import reactive | ||
from shiny.express import input, render, ui | ||
|
||
ui.input_password("password", "Password:") | ||
ui.input_action_button("go", "Go") | ||
|
||
|
||
@render.code | ||
@reactive.event(input.go) | ||
def value(): | ||
return input.password() |
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,15 @@ | ||
from shiny.express import input, render, ui | ||
|
||
ui.input_radio_buttons( | ||
"rb", | ||
"Choose one:", | ||
{ | ||
"html": ui.HTML("<span style='color:red;'>Red Text</span>"), | ||
"text": "Normal text", | ||
}, | ||
) | ||
|
||
|
||
@render.express | ||
def val(): | ||
"You chose " + input.rb() |
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,16 @@ | ||
from shiny.express import input, render, ui | ||
|
||
ui.input_select( | ||
"state", | ||
"Choose a state:", | ||
{ | ||
"East Coast": {"NY": "New York", "NJ": "New Jersey", "CT": "Connecticut"}, | ||
"West Coast": {"WA": "Washington", "OR": "Oregon", "CA": "California"}, | ||
"Midwest": {"MN": "Minnesota", "WI": "Wisconsin", "IA": "Iowa"}, | ||
}, | ||
) | ||
|
||
|
||
@render.text | ||
def value(): | ||
return "You choose: " + str(input.state()) |
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,45 @@ | ||
from html import escape # noqa: F401 | ||
|
||
from shiny.express import input, render, ui | ||
|
||
states = { | ||
"East Coast": {"NY": "New York", "NJ": "New Jersey", "CT": "Connecticut"}, | ||
"West Coast": {"WA": "Washington", "OR": "Oregon", "CA": "California"}, | ||
"Midwest": {"MN": "Minnesota", "WI": "Wisconsin", "IA": "Iowa"}, | ||
} | ||
|
||
ui.input_selectize( | ||
"state", | ||
"Choose a state:", | ||
states, | ||
multiple=True, | ||
) | ||
|
||
|
||
@render.text | ||
def value(): | ||
return "You choose: " + str(input.state()) | ||
|
||
|
||
ui.input_selectize( | ||
"state2", | ||
"Selectize Options", | ||
states, | ||
multiple=True, | ||
options=( | ||
{ | ||
"placeholder": "Enter text", | ||
"render": ui.js_eval( | ||
'{option: function(item, escape) {return "<div><strong>Select " + escape(item.label) + "</strong></div>";}}' | ||
), | ||
"create": True, | ||
} | ||
), | ||
) | ||
ui.input_selectize( | ||
"state3", | ||
"Selectize plugins", | ||
states, | ||
multiple=True, | ||
options={"plugins": ["clear_button"]}, | ||
) |
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
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,16 @@ | ||
import matplotlib.pyplot as plt | ||
import numpy as np | ||
|
||
from shiny.express import input, render, ui | ||
|
||
ui.input_slider("obs", "Number of bins:", min=10, max=100, value=30) | ||
|
||
|
||
@render.plot | ||
def distPlot(): | ||
np.random.seed(19680801) | ||
x = 100 + 15 * np.random.randn(437) | ||
|
||
fig, ax = plt.subplots() | ||
ax.hist(x, input.obs(), density=True) | ||
return fig |
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,8 @@ | ||
from shiny.express import input, render, ui | ||
|
||
ui.input_switch("somevalue", "Some value", False) | ||
|
||
|
||
@render.text | ||
def value(): | ||
return input.somevalue() |
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
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,8 @@ | ||
from shiny.express import input, render, ui | ||
|
||
ui.input_text("caption", "Caption:", "Data summary") | ||
|
||
|
||
@render.code | ||
def value(): | ||
return input.caption() |
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,25 @@ | ||
from shiny.express import input, render, ui | ||
|
||
ui.input_text_area( | ||
"caption_regular", | ||
"Caption:", | ||
"Data summary\nwith\nmultiple\nlines", | ||
) | ||
|
||
|
||
@render.text | ||
def value_regular(): | ||
return input.caption_regular() | ||
|
||
|
||
ui.input_text_area( | ||
"caption_autoresize", | ||
ui.markdown("Caption (w/ `autoresize=True`):"), | ||
"Data summary\nwith\nmultiple\nlines", | ||
autoresize=True, | ||
) | ||
|
||
|
||
@render.text | ||
def value_autoresize(): | ||
return input.caption_autoresize() |