Skip to content

Commit e24d44e

Browse files
author
Gordon Shotwell
authored
expressify-input-examples (#1057)
1 parent 9c5a87c commit e24d44e

File tree

17 files changed

+301
-4
lines changed

17 files changed

+301
-4
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import matplotlib.pyplot as plt
2+
import numpy as np
3+
4+
from shiny import reactive
5+
from shiny.express import input, render, ui
6+
7+
ui.input_slider("n", "Number of observations", min=0, max=1000, value=500)
8+
ui.input_action_link("go", "Go!")
9+
10+
11+
@render.plot(alt="A histogram")
12+
# reactive.event() to invalidate the plot when the button is pressed but not when
13+
# the slider is changed
14+
@reactive.event(input.go, ignore_none=False)
15+
def plot():
16+
np.random.seed(19680801)
17+
x = 100 + 15 * np.random.randn(input.n())
18+
fig, ax = plt.subplots()
19+
ax.hist(x, bins=30, density=True)
20+
return fig
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from shiny.express import input, render, ui
2+
3+
ui.input_checkbox("somevalue", "Some value", False)
4+
5+
6+
@render.ui
7+
def value():
8+
return input.somevalue()
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from shiny import req
2+
from shiny.express import input, render, ui
3+
4+
ui.input_checkbox_group(
5+
"colors",
6+
"Choose color(s):",
7+
{
8+
"red": ui.span("Red", style="color: #FF0000;"),
9+
"green": ui.span("Green", style="color: #00AA00;"),
10+
"blue": ui.span("Blue", style="color: #0000AA;"),
11+
},
12+
)
13+
14+
15+
@render.ui
16+
def val():
17+
req(input.colors())
18+
return "You chose " + ", ".join(input.colors())
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from datetime import date
2+
3+
from shiny.express import ui
4+
5+
ui.input_date("date1", "Date:", value="2016-02-29")
6+
# Default value is the date in client's time zone
7+
ui.input_date("date2", "Date:")
8+
# value is always yyyy-mm-dd, even if the display format is different
9+
ui.input_date("date3", "Date:", value="2016-02-29", format="mm/dd/yy")
10+
# Pass in a Date object
11+
ui.input_date("date4", "Date:", value=date(2016, 2, 29))
12+
# Use different language and different first day of week
13+
ui.input_date("date5", "Date:", language="ru", weekstart=1)
14+
# Start with decade view instead of default month view
15+
ui.input_date("date6", "Date:", startview="decade")
16+
# Disable Mondays and Tuesdays.
17+
ui.input_date("date7", "Date:", daysofweekdisabled=[1, 2])
18+
# Disable specific dates.
19+
ui.input_date(
20+
"date8",
21+
"Date:",
22+
value="2016-02-29",
23+
datesdisabled=["2016-03-01", "2016-03-02"],
24+
)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from datetime import date
2+
3+
from shiny.express import ui
4+
5+
ui.input_date_range("daterange1", "Date range:", start="2001-01-01", end="2010-12-31")
6+
# Default start and end is the current date in the client's time zone
7+
ui.input_date_range("daterange2", "Date range:")
8+
# start and end are always specified in yyyy-mm-dd, even if the display
9+
# format is different
10+
ui.input_date_range(
11+
"daterange3",
12+
"Date range:",
13+
start="2001-01-01",
14+
end="2010-12-31",
15+
min="2001-01-01",
16+
max="2012-12-21",
17+
format="mm/dd/yy",
18+
separator=" - ",
19+
)
20+
# Pass in Date objects
21+
ui.input_date_range(
22+
"daterange4", "Date range:", start=date(2001, 1, 1), end=date(2010, 12, 31)
23+
)
24+
# Use different language and different first day of week
25+
ui.input_date_range("daterange5", "Date range:", language="de", weekstart=1)
26+
# Start with decade view instead of default month view
27+
ui.input_date_range("daterange6", "Date range:", startview="decade")
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import pandas as pd
2+
3+
from shiny import reactive
4+
from shiny.express import input, render, ui
5+
from shiny.types import FileInfo
6+
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"],
13+
)
14+
15+
16+
@reactive.Calc
17+
def parsed_file():
18+
file: list[FileInfo] | None = input.file1()
19+
if file is None:
20+
return pd.DataFrame()
21+
return pd.read_csv(file[0]["datapath"]) # pyright: ignore[reportUnknownMemberType]
22+
23+
24+
@render.table
25+
def summary():
26+
df = parsed_file()
27+
28+
if df.empty:
29+
return pd.DataFrame()
30+
31+
# Get the row count, column count, and column names of the DataFrame
32+
row_count = df.shape[0]
33+
column_count = df.shape[1]
34+
names = df.columns.tolist()
35+
column_names = ", ".join(str(name) for name in names)
36+
37+
# Create a new DataFrame to display the information
38+
info_df = pd.DataFrame(
39+
{
40+
"Row Count": [row_count],
41+
"Column Count": [column_count],
42+
"Column Names": [column_names],
43+
}
44+
)
45+
46+
# input.stats() is a list of strings; subset the columns based on the selected
47+
# checkboxes
48+
return info_df.loc[:, input.stats()]
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from shiny.express import input, render, ui
2+
3+
ui.input_numeric("obs", "Observations:", 10, min=1, max=100)
4+
5+
6+
@render.code
7+
def value():
8+
return input.obs()
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from shiny import reactive
2+
from shiny.express import input, render, ui
3+
4+
ui.input_password("password", "Password:")
5+
ui.input_action_button("go", "Go")
6+
7+
8+
@render.code
9+
@reactive.event(input.go)
10+
def value():
11+
return input.password()
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from shiny.express import input, render, ui
2+
3+
ui.input_radio_buttons(
4+
"rb",
5+
"Choose one:",
6+
{
7+
"html": ui.HTML("<span style='color:red;'>Red Text</span>"),
8+
"text": "Normal text",
9+
},
10+
)
11+
12+
13+
@render.express
14+
def val():
15+
"You chose " + input.rb()
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from shiny.express import input, render, ui
2+
3+
ui.input_select(
4+
"state",
5+
"Choose a state:",
6+
{
7+
"East Coast": {"NY": "New York", "NJ": "New Jersey", "CT": "Connecticut"},
8+
"West Coast": {"WA": "Washington", "OR": "Oregon", "CA": "California"},
9+
"Midwest": {"MN": "Minnesota", "WI": "Wisconsin", "IA": "Iowa"},
10+
},
11+
)
12+
13+
14+
@render.text
15+
def value():
16+
return "You choose: " + str(input.state())

0 commit comments

Comments
 (0)