Spaces:
Runtime error
Runtime error
import streamlit as st | |
from sqlalchemy.orm import sessionmaker | |
from sqlalchemy import create_engine | |
from sqlalchemy import Column, Integer, String, Date, Boolean, Sequence | |
from sqlalchemy.orm import declarative_base | |
from sqlalchemy.sql import text | |
Base = declarative_base() | |
class Customer(Base): | |
__tablename__ = 'customers' | |
id = Column(Integer, Sequence('customers_id_seq'), primary_key=True) | |
first_name = Column(String(50), nullable=False) | |
last_name = Column(String(50), nullable=False) | |
email = Column(String(255), unique=True, nullable=False) | |
phone_number = Column(String(10)) | |
signup_date = Column(Date, server_default=text("(CURRENT_DATE)"), nullable=False) | |
active = Column(Boolean, default=True, nullable=False) | |
# Set up the database | |
engine = create_engine(st.secrets["SQL_CONNECTION_STRING"]) | |
Base.metadata.create_all(engine) | |
# Set up the session | |
Session = sessionmaker(bind=engine) | |
session = Session() | |
# Set up the main interface | |
st.title('Customer Management App') | |
first_name = st.text_input('First Name') | |
last_name = st.text_input('Last Name') | |
email = st.text_input('Email') | |
phone_number = st.text_input('Phone Number') | |
if st.button('Add Customer'): | |
new_customer = Customer(first_name=first_name, last_name=last_name, email=email, phone_number=phone_number) | |
session.add(new_customer) | |
session.commit() | |
if st.button('Show Customers'): | |
customers = session.query(Customer).all() | |
for customer in customers: | |
st.write(f'ID: {customer.id}, First Name: {customer.first_name}, Last Name: {customer.last_name}, Email: {customer.email}, Phone Number: {customer.phone_number}, Signup Date: {customer.signup_date}, Active: {customer.active}') | |
# TODO: add and delete don't work | |
# if st.button('Delete Customer'): | |
# customer_id = st.number_input('Enter the ID of the customer to delete', value=None, min_value=0) | |
# if customer_id: | |
# customer_to_delete = session.query(Customer).get(customer_id) | |
# session.delete(customer_to_delete) | |
# session.commit() | |
# else: | |
# st.write('Customer not found') | |
# | |
# if st.button('Update Customer'): | |
# customer_id = st.number_input('Enter the ID of the customer to update', value=0, min_value=0) | |
# customer_to_update = session.query(Customer).get(customer_id) | |
# if customer_to_update is not None: | |
# customer_to_update.first_name = st.text_input('New First Name') | |
# customer_to_update.last_name = st.text_input('New Last Name') | |
# customer_to_update.email = st.text_input('New Email') | |
# customer_to_update.phone_number = st.text_input('New Phone Number') | |
# session.commit() | |
# else: | |
# st.write('Customer not found') | |