Spaces:
Runtime error
Runtime error
Update src/gsheet.py
Browse files- src/gsheet.py +22 -16
src/gsheet.py
CHANGED
|
@@ -1,36 +1,42 @@
|
|
| 1 |
import gspread
|
| 2 |
-
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
sheet = client.open("Student Marks").sheet1
|
| 7 |
-
|
| 8 |
-
sheet.append_row(["Sujit", "101", 18])
|
| 9 |
|
|
|
|
|
|
|
|
|
|
| 10 |
def connect_sheet():
|
| 11 |
-
scope = [
|
| 12 |
-
"https://spreadsheets.google.com/feeds",
|
| 13 |
-
"https://www.googleapis.com/auth/drive"
|
| 14 |
-
]
|
| 15 |
|
| 16 |
-
|
| 17 |
-
"credentials.json", scope
|
| 18 |
-
)
|
| 19 |
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
|
| 23 |
-
print(client.openall())
|
| 24 |
|
| 25 |
sheet = client.open_by_key(SHEET_ID).sheet1
|
| 26 |
return sheet
|
| 27 |
|
| 28 |
|
|
|
|
|
|
|
|
|
|
| 29 |
def insert_data(data):
|
| 30 |
sheet = connect_sheet()
|
| 31 |
sheet.append_row(data)
|
| 32 |
|
| 33 |
|
|
|
|
|
|
|
|
|
|
| 34 |
def get_all_data():
|
| 35 |
sheet = connect_sheet()
|
| 36 |
return sheet.get_all_records()
|
|
|
|
| 1 |
import gspread
|
| 2 |
+
import os
|
| 3 |
+
import json
|
| 4 |
+
from google.oauth2.service_account import Credentials
|
| 5 |
|
| 6 |
+
SHEET_ID = "1XdzW7Vgd3bYT8f9qTO2vp00OkbdhvhkH5P-23PihXmw"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
# ==================================
|
| 9 |
+
# 🔐 CONNECT USING HUGGING FACE SECRET
|
| 10 |
+
# ==================================
|
| 11 |
def connect_sheet():
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
creds_dict = json.loads(os.environ["GOOGLE_CREDENTIALS"])
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
credentials = Credentials.from_service_account_info(
|
| 16 |
+
creds_dict,
|
| 17 |
+
scopes=[
|
| 18 |
+
"https://www.googleapis.com/auth/spreadsheets",
|
| 19 |
+
"https://www.googleapis.com/auth/drive"
|
| 20 |
+
]
|
| 21 |
+
)
|
| 22 |
|
| 23 |
+
client = gspread.authorize(credentials)
|
|
|
|
| 24 |
|
| 25 |
sheet = client.open_by_key(SHEET_ID).sheet1
|
| 26 |
return sheet
|
| 27 |
|
| 28 |
|
| 29 |
+
# ==================================
|
| 30 |
+
# 📥 INSERT DATA
|
| 31 |
+
# ==================================
|
| 32 |
def insert_data(data):
|
| 33 |
sheet = connect_sheet()
|
| 34 |
sheet.append_row(data)
|
| 35 |
|
| 36 |
|
| 37 |
+
# ==================================
|
| 38 |
+
# 📤 GET ALL DATA
|
| 39 |
+
# ==================================
|
| 40 |
def get_all_data():
|
| 41 |
sheet = connect_sheet()
|
| 42 |
return sheet.get_all_records()
|