Spaces:
Runtime error
Runtime error
Update app.py
#2
by
Vinayak221
- opened
app.py
CHANGED
@@ -1,151 +1,58 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
import
|
4 |
-
|
5 |
-
import
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
ui.sidebar(
|
24 |
-
# Artwork by @allison_horst
|
25 |
-
ui.input_selectize(
|
26 |
-
"xvar",
|
27 |
-
"X variable",
|
28 |
-
numeric_cols,
|
29 |
-
selected="Bill Length (mm)",
|
30 |
-
),
|
31 |
-
ui.input_selectize(
|
32 |
-
"yvar",
|
33 |
-
"Y variable",
|
34 |
-
numeric_cols,
|
35 |
-
selected="Bill Depth (mm)",
|
36 |
-
),
|
37 |
-
ui.input_checkbox_group(
|
38 |
-
"species", "Filter by species", species, selected=species
|
39 |
-
),
|
40 |
-
ui.hr(),
|
41 |
-
ui.input_switch("by_species", "Show species", value=True),
|
42 |
-
ui.input_switch("show_margins", "Show marginal plots", value=True),
|
43 |
-
),
|
44 |
-
ui.output_ui("value_boxes"),
|
45 |
-
ui.output_plot("scatter", fill=True),
|
46 |
-
ui.help_text(
|
47 |
-
"Artwork by ",
|
48 |
-
ui.a("@allison_horst", href="https://twitter.com/allison_horst"),
|
49 |
-
class_="text-end",
|
50 |
-
),
|
51 |
-
),
|
52 |
)
|
53 |
|
54 |
-
|
55 |
-
def
|
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 |
-
@render.ui
|
86 |
-
def value_boxes():
|
87 |
-
df = filtered_df()
|
88 |
-
|
89 |
-
def penguin_value_box(title: str, count: int, bgcol: str, showcase_img: str):
|
90 |
-
return ui.value_box(
|
91 |
-
title,
|
92 |
-
count,
|
93 |
-
{"class_": "pt-1 pb-0"},
|
94 |
-
showcase=ui.fill.as_fill_item(
|
95 |
-
ui.tags.img(
|
96 |
-
{"style": "object-fit:contain;"},
|
97 |
-
src=showcase_img,
|
98 |
-
)
|
99 |
-
),
|
100 |
-
theme_color=None,
|
101 |
-
style=f"background-color: {bgcol};",
|
102 |
-
)
|
103 |
-
|
104 |
-
if not input.by_species():
|
105 |
-
return penguin_value_box(
|
106 |
-
"Penguins",
|
107 |
-
len(df.index),
|
108 |
-
bg_palette["default"],
|
109 |
-
# Artwork by @allison_horst
|
110 |
-
showcase_img="penguins.png",
|
111 |
-
)
|
112 |
-
|
113 |
-
value_boxes = [
|
114 |
-
penguin_value_box(
|
115 |
-
name,
|
116 |
-
len(df[df["Species"] == name]),
|
117 |
-
bg_palette[name],
|
118 |
-
# Artwork by @allison_horst
|
119 |
-
showcase_img=f"{name}.png",
|
120 |
-
)
|
121 |
-
for name in species
|
122 |
-
# Only include boxes for _selected_ species
|
123 |
-
if name in input.species()
|
124 |
-
]
|
125 |
-
|
126 |
-
return ui.layout_column_wrap(*value_boxes, width = 1 / len(value_boxes))
|
127 |
-
|
128 |
-
|
129 |
-
# "darkorange", "purple", "cyan4"
|
130 |
-
colors = [[255, 140, 0], [160, 32, 240], [0, 139, 139]]
|
131 |
-
colors = [(r / 255.0, g / 255.0, b / 255.0) for r, g, b in colors]
|
132 |
-
|
133 |
-
palette: Dict[str, Tuple[float, float, float]] = {
|
134 |
-
"Adelie": colors[0],
|
135 |
-
"Chinstrap": colors[1],
|
136 |
-
"Gentoo": colors[2],
|
137 |
-
"default": sns.color_palette()[0], # type: ignore
|
138 |
-
}
|
139 |
-
|
140 |
-
bg_palette = {}
|
141 |
-
# Use `sns.set_style("whitegrid")` to help find approx alpha value
|
142 |
-
for name, col in palette.items():
|
143 |
-
# Adjusted n_colors until `axe` accessibility did not complain about color contrast
|
144 |
-
bg_palette[name] = mpl_colors.to_hex(sns.light_palette(col, n_colors=7)[1]) # type: ignore
|
145 |
-
|
146 |
-
|
147 |
-
app = App(
|
148 |
-
app_ui,
|
149 |
-
server,
|
150 |
-
static_assets=str(www_dir),
|
151 |
)
|
|
|
|
|
|
1 |
+
# app.py
|
2 |
+
|
3 |
+
from shiny import App, render, ui
|
4 |
+
from shiny.flask import FlaskApp
|
5 |
+
from flask import Flask, redirect, url_for, session, request
|
6 |
+
from authlib.integrations.flask_client import OAuth
|
7 |
+
|
8 |
+
app = Flask(__name__)
|
9 |
+
app.secret_key = 'your_secret_key'
|
10 |
+
|
11 |
+
oauth = OAuth(app)
|
12 |
+
google = oauth.register(
|
13 |
+
name='google',
|
14 |
+
client_id='your_client_id',
|
15 |
+
client_secret='your_client_secret',
|
16 |
+
authorize_url='https://accounts.google.com/o/oauth2/auth',
|
17 |
+
authorize_params=None,
|
18 |
+
authorize_params={'scope': 'openid email profile'},
|
19 |
+
access_token_url='https://accounts.google.com/o/oauth2/token',
|
20 |
+
access_token_params=None,
|
21 |
+
refresh_token_url=None,
|
22 |
+
client_kwargs={'scope': 'openid email profile'},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
)
|
24 |
|
25 |
+
@app.route('/login')
|
26 |
+
def login():
|
27 |
+
redirect_uri = url_for('authorize', _external=True)
|
28 |
+
return google.authorize_redirect(redirect_uri)
|
29 |
+
|
30 |
+
@app.route('/authorize')
|
31 |
+
def authorize():
|
32 |
+
token = google.authorize_access_token()
|
33 |
+
user = google.parse_id_token(token)
|
34 |
+
session['user'] = user
|
35 |
+
return redirect(url_for('index'))
|
36 |
+
|
37 |
+
@app.route('/logout')
|
38 |
+
def logout():
|
39 |
+
del session['user']
|
40 |
+
return redirect(url_for('index'))
|
41 |
+
|
42 |
+
@app.route('/')
|
43 |
+
def index():
|
44 |
+
user = session.get('user')
|
45 |
+
if user:
|
46 |
+
username = user['name']
|
47 |
+
else:
|
48 |
+
username = "Guest"
|
49 |
+
return f'Hello, {username}!'
|
50 |
+
|
51 |
+
# Embed Flask app within shiny-python
|
52 |
+
shiny_app = App(
|
53 |
+
ui.page(ui.title("Google OAuth Example")),
|
54 |
+
render.FlexDashboard("flex_dashboard", title="Google OAuth Example"),
|
55 |
+
FlaskApp(app)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
)
|
57 |
+
|
58 |
+
shiny_app.serve()
|