Permalink
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Penguins-ShinyPy/app.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
62 lines (49 sloc)
2.73 KB
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
import plotly_express as px | |
from faicons import icon_svg | |
import plotly.graph_objs as go | |
from shinywidgets import output_widget, render_widget | |
# Import data from shared.py | |
from shared import app_dir, df | |
from shiny import App, reactive, render, ui | |
app_ui = ui.page_sidebar(ui.sidebar(ui.input_slider("mass", "Mass", 2000, 6000, 6000), | |
ui.input_checkbox_group("species", "Species", ["Adelie", "Gentoo", "Chinstrap"], | |
selected=["Adelie", "Gentoo", "Chinstrap"], ), | |
title="Filter controls", ), ui.layout_column_wrap( | |
ui.value_box("Number of penguins", ui.output_text("count"), showcase=icon_svg("earlybirds"), ), | |
ui.value_box("Average bill length", ui.output_text("bill_length"), showcase=icon_svg("ruler-horizontal"), ), | |
ui.value_box("Average bill depth", ui.output_text("bill_depth"), showcase=icon_svg("ruler-vertical"), ), | |
fill=False, ), ui.layout_columns( | |
ui.card(ui.card_header("Bill length and depth"), output_widget("length_depth"), full_screen=True, ), | |
ui.card(ui.card_header("Species and Habitat"), output_widget("species_island"), full_screen=True), | |
ui.card(ui.card_header("Penguin data"), ui.output_data_frame("summary_statistics"), full_screen=True, ), ), | |
ui.include_css(app_dir / "styles.css"), title="Penguins dashboard", fillable=True, ) | |
def server(input, output, session): | |
@reactive.calc | |
def filtered_df(): | |
filt_df = df[df["species"].isin(input.species())] | |
filt_df = filt_df.loc[filt_df["body_mass_g"] < input.mass()] | |
return filt_df | |
@render.text | |
def count(): | |
return filtered_df().shape[0] | |
@render.text | |
def bill_length(): | |
return f"{filtered_df()['bill_length_mm'].mean():.1f} mm" | |
@render.text | |
def bill_depth(): | |
return f"{filtered_df()['bill_depth_mm'].mean():.1f} mm" | |
@render_widget | |
def length_depth(): | |
fig = px.scatter(data_frame=filtered_df(), x='bill_length_mm', y='bill_depth_mm', color='species') | |
fig.update_layout(dict(plot_bgcolor='rgba(0, 0, 0, 0)'), xaxis=dict(showgrid=False), yaxis=dict(showgrid=False)) | |
return go.FigureWidget(fig) | |
@render_widget | |
def species_island(): | |
fig = px.bar(data_frame=filtered_df(), x='species', color='island', barmode='group') | |
fig.update_layout(dict(plot_bgcolor='rgba(0, 0, 0, 0)'), xaxis=dict(showgrid=False), yaxis=dict(showgrid=False)) | |
return go.FigureWidget(fig) | |
@render.data_frame | |
def summary_statistics(): | |
cols = ["species", "island", "bill_length_mm", "bill_depth_mm", "body_mass_g", ] | |
return render.DataGrid(filtered_df()[cols], filters=True) | |
app = App(app_ui, server) |