-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp1.py
59 lines (49 loc) · 2.14 KB
/
app1.py
1
2
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import gspread
from oauth2client.service_account import ServiceAccountCredentials
import streamlit as st
import pandas as pd
# Authenticate and connect to Google Sheets
def connect_to_gsheet(creds_json, spreadsheet_name, sheet_name):
scope = ["https://spreadsheets.google.com/feeds",
'https://www.googleapis.com/auth/spreadsheets',
"https://www.googleapis.com/auth/drive.file",
"https://www.googleapis.com/auth/drive"]
credentials = ServiceAccountCredentials.from_json_keyfile_name(creds_json, scope)
client = gspread.authorize(credentials)
spreadsheet = client.open(spreadsheet_name)
return spreadsheet.worksheet(sheet_name) # Access specific sheet by name
# Google Sheet credentials and details
SPREADSHEET_NAME = 'Streamlit'
SHEET_NAME = 'Sheet1'
CREDENTIALS_FILE = './credentials.json'
# Connect to the Google Sheet
sheet_by_name = connect_to_gsheet(CREDENTIALS_FILE, SPREADSHEET_NAME, sheet_name=SHEET_NAME)
st.title("Simple Data Entry using Streamlit")
# Read Data from Google Sheets
def read_data():
data = sheet_by_name.get_all_records() # Get all records from Google Sheet
return pd.DataFrame(data)
# Add Data to Google Sheets
def add_data(row):
sheet_by_name.append_row(row) # Append the row to the Google Sheet
# Sidebar form for data entry
with st.sidebar:
st.header("Enter New Data")
# Assuming the sheet has columns: 'Name', 'Age', 'Email'
with st.form(key="data_form"):
name = st.text_input("Name")
age = st.number_input("Age", min_value=0, max_value=120)
email = st.text_input("Email")
# Submit button inside the form
submitted = st.form_submit_button("Submit")
# Handle form submission
if submitted:
if name and email: # Basic validation to check if required fields are filled
add_data([name, age, email]) # Append the row to the sheet
st.success("Data added successfully!")
else:
st.error("Please fill out the form correctly.")
# Display data in the main view
st.header("Data Table")
df = read_data()
st.dataframe(df, width=800, height=400)