tt24 commited on
Commit
d6752f4
·
verified ·
1 Parent(s): 68f356b
Files changed (1) hide show
  1. app.py +0 -154
app.py CHANGED
@@ -1,155 +1 @@
1
  print("00000")
2
-
3
- """
4
- from pathlib import Path
5
- from typing import List, Dict, Tuple
6
- import matplotlib.colors as mpl_colors
7
-
8
- import pandas as pd
9
- import seaborn as sns
10
- import shinyswatch
11
-
12
- from shiny import App, Inputs, Outputs, Session, reactive, render, req, ui
13
-
14
- sns.set_theme()
15
-
16
- www_dir = Path(__file__).parent.resolve() / "www"
17
-
18
- df = pd.read_csv(Path(__file__).parent / "penguins.csv", na_values="NA")
19
- numeric_cols: List[str] = df.select_dtypes(include=["float64"]).columns.tolist()
20
- species: List[str] = df["Species"].unique().tolist()
21
- species.sort()
22
-
23
- app_ui = ui.page_fillable(
24
- shinyswatch.theme.minty(),
25
- ui.layout_sidebar(
26
- ui.sidebar(
27
- # Artwork by @allison_horst
28
- ui.input_selectize(
29
- "xvar",
30
- "X variable",
31
- numeric_cols,
32
- selected="Bill Length (mm)",
33
- ),
34
- ui.input_selectize(
35
- "yvar",
36
- "Y variable",
37
- numeric_cols,
38
- selected="Bill Depth (mm)",
39
- ),
40
- ui.input_checkbox_group(
41
- "species", "Filter by species", species, selected=species
42
- ),
43
- ui.hr(),
44
- ui.input_switch("by_species", "Show species", value=True),
45
- ui.input_switch("show_margins", "Show marginal plots", value=True),
46
- ),
47
- ui.output_ui("value_boxes"),
48
- ui.output_plot("scatter", fill=True),
49
- ui.help_text(
50
- "Artwork by ",
51
- ui.a("@allison_horst", href="https://twitter.com/allison_horst"),
52
- class_="text-end",
53
- ),
54
- ),
55
- )
56
-
57
-
58
- def server(input: Inputs, output: Outputs, session: Session):
59
- @reactive.Calc
60
- def filtered_df() -> pd.DataFrame:
61
- """Returns a Pandas data frame that includes only the desired rows"""
62
-
63
- # This calculation "req"uires that at least one species is selected
64
- req(len(input.species()) > 0)
65
-
66
- # Filter the rows so we only include the desired species
67
- return df[df["Species"].isin(input.species())]
68
-
69
- @output
70
- @render.plot
71
- def scatter():
72
- """Generates a plot for Shiny to display to the user"""
73
-
74
- # The plotting function to use depends on whether margins are desired
75
- plotfunc = sns.jointplot if input.show_margins() else sns.scatterplot
76
-
77
- plotfunc(
78
- data=filtered_df(),
79
- x=input.xvar(),
80
- y=input.yvar(),
81
- palette=palette,
82
- hue="Species" if input.by_species() else None,
83
- hue_order=species,
84
- legend=False,
85
- )
86
-
87
- @output
88
- @render.ui
89
- def value_boxes():
90
- df = filtered_df()
91
-
92
- def penguin_value_box(title: str, count: int, bgcol: str, showcase_img: str):
93
- return ui.value_box(
94
- title,
95
- count,
96
- {"class_": "pt-1 pb-0"},
97
- showcase=ui.fill.as_fill_item(
98
- ui.tags.img(
99
- {"style": "object-fit:contain;"},
100
- src=showcase_img,
101
- )
102
- ),
103
- theme_color=None,
104
- style=f"background-color: {bgcol};",
105
- )
106
-
107
- if not input.by_species():
108
- return penguin_value_box(
109
- "Penguins",
110
- len(df.index),
111
- bg_palette["default"],
112
- # Artwork by @allison_horst
113
- showcase_img="penguins.png",
114
- )
115
-
116
- value_boxes = [
117
- penguin_value_box(
118
- name,
119
- len(df[df["Species"] == name]),
120
- bg_palette[name],
121
- # Artwork by @allison_horst
122
- showcase_img=f"{name}.png",
123
- )
124
- for name in species
125
- # Only include boxes for _selected_ species
126
- if name in input.species()
127
- ]
128
-
129
- return ui.layout_column_wrap(*value_boxes, width = 1 / len(value_boxes))
130
-
131
-
132
- # "darkorange", "purple", "cyan4"
133
- colors = [[255, 140, 0], [160, 32, 240], [0, 139, 139]]
134
- colors = [(r / 255.0, g / 255.0, b / 255.0) for r, g, b in colors]
135
-
136
- palette: Dict[str, Tuple[float, float, float]] = {
137
- "Adelie": colors[0],
138
- "Chinstrap": colors[1],
139
- "Gentoo": colors[2],
140
- "default": sns.color_palette()[0], # type: ignore
141
- }
142
-
143
- bg_palette = {}
144
- # Use `sns.set_style("whitegrid")` to help find approx alpha value
145
- for name, col in palette.items():
146
- # Adjusted n_colors until `axe` accessibility did not complain about color contrast
147
- bg_palette[name] = mpl_colors.to_hex(sns.light_palette(col, n_colors=7)[1]) # type: ignore
148
-
149
-
150
- app = App(
151
- app_ui,
152
- server,
153
- static_assets=str(www_dir),
154
- )
155
- """
 
1
  print("00000")