osaaso commited on
Commit
c8f2b68
·
1 Parent(s): 6a07bf0

Update app.py

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