ayushnoori commited on
Commit
0cd2128
·
1 Parent(s): 01b734e

Update user auth for HF spaces

Browse files
Files changed (2) hide show
  1. app.py +49 -24
  2. requirements.txt +2 -1
app.py CHANGED
@@ -1,28 +1,15 @@
1
  import streamlit as st
2
- from streamlit_gsheets import GSheetsConnection
 
 
 
3
  import hmac
4
 
5
  # Standard imports
6
- import numpy as np
7
  import pandas as pd
8
- from datetime import datetime
9
- import os
10
- import random
11
-
12
- # Import torch
13
- import torch
14
-
15
- # Data visualization
16
- import matplotlib.pyplot as plt
17
- import seaborn as sns
18
-
19
- # Path manipulation
20
- from pathlib import Path
21
- import sys
22
 
23
  # Custom and other imports
24
- import project_config
25
- from utils import add_logo
26
  from menu import menu
27
 
28
  # Insert logo
@@ -39,6 +26,27 @@ if "role" not in st.session_state:
39
  # # Callback function to save the role selection to Session State
40
  # st.session_state.role = st.session_state._role
41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
  def check_password():
43
  """Returns `True` if the user had a correct password."""
44
 
@@ -51,14 +59,31 @@ def check_password():
51
 
52
  def password_entered():
53
  """Checks whether a password entered by the user is correct."""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
 
55
- # Create a connection object to Google Sheets
56
- conn = st.connection("gsheets", type=GSheetsConnection)
57
 
58
- # Read the user database
59
- user_db = conn.read()
60
- user_db.dropna(axis=0, how="all", inplace=True)
61
- user_db.dropna(axis=1, how="all", inplace=True)
62
 
63
  # Check if the username is in the database
64
  if st.session_state["username"] in user_db.username.values:
 
1
  import streamlit as st
2
+ # Do not load st-gsheets-connection
3
+ # from streamlit_gsheets import GSheetsConnection
4
+ import gspread
5
+ from oauth2client.service_account import ServiceAccountCredentials
6
  import hmac
7
 
8
  # Standard imports
 
9
  import pandas as pd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
  # Custom and other imports
12
+ # from utils import add_logo
 
13
  from menu import menu
14
 
15
  # Insert logo
 
26
  # # Callback function to save the role selection to Session State
27
  # st.session_state.role = st.session_state._role
28
 
29
+
30
+ # From https://stackoverflow.com/questions/55961295/serviceaccountcredentials-from-json-keyfile-name-equivalent-for-remote-json
31
+ # See also https://www.slingacademy.com/article/pandas-how-to-read-and-update-google-sheet-files/
32
+ def create_keyfile_dict():
33
+ variables_keys = {
34
+ # "spreadsheet": st.secrets['spreadsheet'], # spreadsheet
35
+ "type": st.secrets['type'], # type
36
+ "project_id": st.secrets['project_id'], # project_id
37
+ "private_key_id": st.secrets['private_key_id'], # private_key_id
38
+ "private_key": st.secrets['private_key'], # private_key
39
+ "client_email": st.secrets['client_email'], # client_email
40
+ "client_id": st.secrets['client_id'], # client_id
41
+ "auth_uri": st.secrets['auth_uri'], # auth_uri
42
+ "token_uri": st.secrets['token_uri'], # token_uri
43
+ "auth_provider_x509_cert_url": st.secrets['auth_provider_x509_cert_url'], # auth_provider_x509_cert_url
44
+ "client_x509_cert_url": st.secrets['client_x509_cert_url'], # client_x509_cert_url
45
+ "universe_domain": st.secrets['universe_domain'] # universe_domain
46
+ }
47
+ return variables_keys
48
+
49
+
50
  def check_password():
51
  """Returns `True` if the user had a correct password."""
52
 
 
59
 
60
  def password_entered():
61
  """Checks whether a password entered by the user is correct."""
62
+
63
+ # Define the scope
64
+ scope = [
65
+ 'https://spreadsheets.google.com/feeds',
66
+ 'https://www.googleapis.com/auth/drive'
67
+ ]
68
+
69
+ # Add credentials to the account
70
+ creds = ServiceAccountCredentials.from_json_keyfile_dict(create_keyfile_dict(), scope)
71
+
72
+ # Authenticate and create the client
73
+ client = gspread.authorize(creds)
74
+
75
+ # Open the spreadsheet
76
+ sheet = client.open_by_url(st.secrets['spreadsheet']).worksheet("user_db")
77
+ data = sheet.get_all_records()
78
+ user_db = pd.DataFrame(data)
79
 
80
+ # # Create a connection object to Google Sheets
81
+ # conn = st.connection("gsheets", type=GSheetsConnection)
82
 
83
+ # # Read the user database
84
+ # user_db = conn.read()
85
+ # user_db.dropna(axis=0, how="all", inplace=True)
86
+ # user_db.dropna(axis=1, how="all", inplace=True)
87
 
88
  # Check if the username is in the database
89
  if st.session_state["username"] in user_db.username.values:
requirements.txt CHANGED
@@ -6,4 +6,5 @@ seaborn
6
  pathlib
7
  torch
8
  altair<5
9
- st-gsheets-connection
 
 
6
  pathlib
7
  torch
8
  altair<5
9
+ gspread
10
+ oauth2client