-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
137 lines (114 loc) · 5.07 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import plotly.express as px
from shiny.express import input, ui
from shinywidgets import render_plotly
import palmerpenguins # This package provides the Palmer Penguins dataset
import pandas as pd
import seaborn as sns
from shiny import render
# Use the built-in function to load the Palmer Penguins dataset
penguins_df = palmerpenguins.load_penguins()
ui.page_opts(title="Penguin Data", fillable=True)
# Add a Shiny UI sidebar for user interaction
# Use the ui.sidebar() function to create a sidebar
# Set the open parameter to "open" to make the sidebar open by default
# Use a with block to add content to the sidebar
with ui.sidebar(open="open"):
# Use the ui.h2() function to add a 2nd level header to the sidebar
# pass in a string argument (in quotes) to set the header text to "Sidebar"
ui.h2("Sidebar")
# Use ui.input_selectize() to create a dropdown input to choose a column
# pass in three arguments:
# the name of the input (in quotes), e.g., "selected_attribute"
# the label for the input (in quotes)
# a list of options for the input (in square brackets)
# e.g. ["bill_length_mm", "bill_depth_mm", "flipper_length_mm", "body_mass_g"]
ui.input_selectize(
"selected_attribute",
"Select Plotly Attribute",
["bill_length_mm", "bill_depth_mm", "flipper_length_mm", "body_mass_g"]
)
# Use ui.input_numeric() to create a numeric input for the number of Plotly histogram bins
# pass in two arguments:
# the name of the input (in quotes), e.g. "plotly_bin_count"
# the label for the input (in quotes)
ui.input_numeric("plotly_bin_count", "Plotly Bin Count", 30)
# Use ui.input_slider() to create a slider input for the number of Seaborn bins
# pass in four arguments:
# the name of the input (in quotes), e.g. "seaborn_bin_count"
# the label for the input (in quotes)
# the minimum value for the input (as an integer)
# the maximum value for the input (as an integer)
# the default value for the input (as an integer)
ui.input_slider("seaborn_bin_count", "Number of Seaborn Bins", 1, 50, 25)
# Use ui.input_checkbox_group() to create a checkbox group input to filter the species
# pass in five arguments:
# the name of the input (in quotes), e.g. "selected_species_list"
# the label for the input (in quotes)
# a list of options for the input (in square brackets) as ["Adelie", "Gentoo", "Chinstrap"]
# a keyword argument selected= a list of selected options for the input (in square brackets)
# a keyword argument inline= a Boolean value (True or False) as you like
ui.input_checkbox_group(
"selected_species_list",
"Species",
["Adelie", "Gentoo", "Chinstrap"],
selected="Gentoo",
inline=True
)
# Use ui.hr() to add a horizontal rule to the sidebar
ui.hr()
# Use ui.a() to add a hyperlink to the sidebar
# pass in two arguments:
# the text for the hyperlink (in quotes), e.g. "GitHub"
# a keyword argument href= the URL for the hyperlink (in quotes), e.g. your GitHub repo URL
# a keyword argument target= "_blank" to open the link in a new tab
ui.a(
"Github",
href="https://github.com/matthewellishanson/cintel-02-data/blob/main/app.py",
target="_blank"
)
# Create a DataTable
with ui.layout_columns():
with ui.card(full_screen=True):
ui.h2("Penguins DataTable")
@render.data_frame
def render_penguins_table():
return render.DataTable(penguins_df)
# create a DataGrid
with ui.card(full_screen=True):
ui.h2("Penguins DataGrid")
@render.data_frame
def penguins_datagrid():
return render.DataGrid(penguins_df)
# Create a Plotly Histogram showing all species
with ui.card(full_screen=True):
ui.card_header("Plotly Histogram")
@render_plotly
def plotly_histogram():
return px.histogram(
penguins_df, x=input.selected_attribute(), nbins=input.plotly_bin_count()
)
# Creates a Seaborn Histogram showing all species
with ui.card(full_screen=True):
ui.card_header("Seaborn Histogram")
@render.plot(alt="Seaborn Histogram")
def seaborn_histogram():
histplot = sns.histplot(data=penguins_df, x="body_mass_g", bins=input.seaborn_bin_count())
histplot.set_title("Palmer Penguins")
histplot.set_xlabel("Mass")
histplot.set_ylabel("Count")
return histplot
# Creates a Plotly Scatterplot showing all species
with ui.card(full_screen=True):
ui.card_header("Plotly Scatterplot: Species")
@render_plotly
def plotly_scatterplot():
return px.scatter(penguins_df,
x="bill_length_mm",
y="body_mass_g",
color="species",
title="Penguins Plot",
labels={
"bill_length_mm": "Bill Length (mm)",
"body_mass_g": "Body Mass (g)",
},
)