Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,64 +1,37 @@
|
|
| 1 |
-
from shiny import App, render,
|
| 2 |
-
from
|
| 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 |
-
token = google.authorize_access_token()
|
| 28 |
-
user = google.parse_id_token(token)
|
| 29 |
-
session['user'] = user
|
| 30 |
-
return redirect('/')
|
| 31 |
-
|
| 32 |
-
@app.route('/logout')
|
| 33 |
-
def logout():
|
| 34 |
-
del session['user']
|
| 35 |
-
return redirect('/')
|
| 36 |
|
| 37 |
-
|
| 38 |
-
def index():
|
| 39 |
-
user = session.get('user')
|
| 40 |
-
if user:
|
| 41 |
-
username = user['name']
|
| 42 |
-
else:
|
| 43 |
-
username = "Guest"
|
| 44 |
-
return f'Hello, {username}!'
|
| 45 |
-
|
| 46 |
-
@app.route('/get_username')
|
| 47 |
-
def get_username():
|
| 48 |
-
user = session.get('user')
|
| 49 |
-
if user:
|
| 50 |
-
return user['name']
|
| 51 |
-
else:
|
| 52 |
-
return "Guest"
|
| 53 |
-
|
| 54 |
-
# Create shiny-python app
|
| 55 |
-
shiny_app = App(
|
| 56 |
-
ui.title("Shiny with Flask Example"),
|
| 57 |
-
ui.page(
|
| 58 |
-
ui.iframe("/flask", height="500px")
|
| 59 |
-
),
|
| 60 |
-
render.FlexDashboard("flex_dashboard", title="Google OAuth Example"),
|
| 61 |
-
FlaskApp(app)
|
| 62 |
-
)
|
| 63 |
|
| 64 |
-
|
|
|
|
|
|
| 1 |
+
from shiny import App, ui, reactive, render, Session
|
| 2 |
+
from google_auth_oauthlib.flow import InstalledAppFlow
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
# Google OAuth Configuration
|
| 6 |
+
CLIENT_SECRETS_FILE = "client_secrets.json"
|
| 7 |
+
SCOPES = ['https://www.googleapis.com/auth/userinfo.email', 'https://www.googleapis.com/auth/userinfo.profile']
|
| 8 |
+
os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = '1' # Enable non-HTTPS for local testing
|
| 9 |
+
|
| 10 |
+
def authenticate_user():
|
| 11 |
+
"""Perform user authentication via Google OAuth and return user info."""
|
| 12 |
+
flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRETS_FILE, SCOPES)
|
| 13 |
+
creds = flow.run_local_server(port=0)
|
| 14 |
+
user_info = creds.id_token
|
| 15 |
+
return user_info
|
| 16 |
+
|
| 17 |
+
app_ui = ui.page_fluid(
|
| 18 |
+
ui.layout_sidebar(
|
| 19 |
+
ui.sidebar(
|
| 20 |
+
ui.input_action_button("login", "Log in with Google"),
|
| 21 |
+
),
|
| 22 |
+
ui.output_text_verbatim("username", placeholder="Username will appear here after login")
|
| 23 |
+
)
|
| 24 |
)
|
| 25 |
|
| 26 |
+
def server(input: Inputs, output: Outputs, session: Session):
|
| 27 |
+
@output
|
| 28 |
+
@render.text
|
| 29 |
+
def username():
|
| 30 |
+
if input.login() == 1:
|
| 31 |
+
user_info = authenticate_user()
|
| 32 |
+
return f"Username: {user_info['email']}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
+
app = App(app_ui, server)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
+
if __name__ == "__main__":
|
| 37 |
+
app.run()
|