Example : Save codes in Streamlit’s  Python using the SQLAlchemy/ORM method using XAMPP’s MySQL,
'''NB: This method is the best for Handling large databases with an ORM approach.First of all, Install SQLAlchemy using Command Prompt/Pycharm Terminal -

    pip install sqlalchemy pymysql streamlit(press enter)
-------------------------------------------------------------------'''

import datetime
from datetime import date
from sqlalchemy import create_engine, text
import streamlit as st

# Database connectivity code using SQLAlchemy technique
engine = create_engine("mysql+pymysql://root:@localhost/cakebake")
st.success("Database Connected Successfully")

# Streamlit UI Form design codes
st.title("User Registration Form")
with st.form("form1"):
    name1 = st.text_input("Enter Your Name", max_chars=30, key="name2")

    #age1 = st.number_input("Enter Your Age", min_value=18, key="age2")
    #age1 = st.number_input("Enter Your Age", key="age2")
    age1 = st.number_input("Enter Your Age", value=0, key="age2")

    email1 = st.text_input("Enter Your Email", key="email2")
    gender1 = st.radio("Select Your Gender", ["Male", "Female", "Transgender"], key="gender2")
    passwd1 = st.text_input('Enter Password', type="password", key="passwd2")
    addr1 = st.text_area('Enter Your Address', height=150, key="addr2")

    dob1 = st.date_input("Choose Your Birth Date", key="dob2", value=date.today())
    #dob1 = st.date_input("Choose your Date of Birth", key="dob2")
    #dob3 = dob1.strftime("%d-%m-%Y") # Format date as DD-MM-YYYY
    #dob3 = dob1.strftime("%d-%b-%Y") # Format date as DD-MMM-YYYY

    nat1 = st.selectbox("Select Your Nationality", ('India', 'USA', 'Nepal', 'Bhutan', 'Sri Lanka'), key="nat2")

    st.write("Choose Your Qualification")
    matric1 = st.checkbox("Matric", key="matric2")
    inter1 = st.checkbox("Intermediate", key="inter2")
    grad1 = st.checkbox("Graduation", key="grad2")

    rem1 = st.text_input("Enter Your Remarks, if any", value="N/A", key="rem2")

    # To set buttons in one row(by default vertical)
    col1, col2, col3, col4, col5 = st.columns(5)
    with col1:
        if st.form_submit_button("Submit"):
            with engine.connect() as conn:
                query = text("INSERT INTO registration (name5, age5, email5, gen5, pass5, addr5, dob5, nat5, matric5, inter5, grad5, rem5) VALUES (:name1, :age1, :email1, :gender1, :passwd1, :addr1, :dob1, :nat1, :matric1, :inter1, :grad1, :rem1)")
                # Execute Query with parameter substitution
                conn.execute(query,{"name1": name1, "age1": age1, "email1": email1, "gender1": gender1, "passwd1": passwd1, "addr1": addr1, "dob1": dob1, "nat1": nat1, "matric1": matric1, "inter1": inter1, "grad1": grad1, "rem1": rem1})
                conn.commit()
                st.success("Data inserted successfully!")
    with col2:
        reset1 = st.form_submit_button("Reset")
    with col3:
        delete1 = st.form_submit_button("Delete")
    with col4:
        update1 = st.form_submit_button("Update")
    with col5:
        exit1 = st.form_submit_button("Exit") st.form_submit_button("Exit")

Loading

Categories: Streamlit

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.