forked from techwithtim/Flask-Web-App-Tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
62 lines (50 loc) · 1.57 KB
/
main.py
File metadata and controls
62 lines (50 loc) · 1.57 KB
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
59
60
61
62
import sqlite3
import streamlit as st
def initialize_db():
conn = sqlite3.connect("my_database.db")
cursor = conn.cursor()
# Create the `users` table if it doesn't exist
cursor.execute("""
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
age INTEGER NOT NULL
)
""")
conn.commit()
conn.close()
# Database connection
def get_db_connection():
conn = sqlite3.connect("my_database.db")
conn.row_factory = sqlite3.Row
return conn
# Streamlit app
def main():
# Initialize the database
initialize_db()
# Connect to the database
conn = get_db_connection()
cursor = conn.cursor()
# Fetch data from the users table
cursor.execute("SELECT * FROM users")
rows = cursor.fetchall()
# Convert sqlite3.Row to dictionary for display
st.subheader("Current Data")
formatted_rows = [dict(row) for row in rows] # Convert each row to a dictionary
for row in formatted_rows:
st.write(row)
# Add new data
st.subheader("Add New User")
name = st.text_input("Name")
age = st.number_input("Age", min_value=0, max_value=120, step=1)
if st.button("Add User"):
if name and age:
cursor.execute("INSERT INTO users (name, age) VALUES (?, ?)", (name, age))
conn.commit()
st.success("User added successfully!")
st.rerun() # Optional: Only if supported
else:
st.error("Please provide valid inputs.")
conn.close()
if __name__ == "__main__":
main()