Example : A Streamlit Python program to hang specific database field’s data in a drop-down list/combo box of UI from a Xampp’s MySql database.
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")

# Function to fetch all emails from XAMPP's MySQL database
def hang():
    query = text("SELECT distinct email5 FROM registration")
    with engine.connect() as conn:
        result = conn.execute(query)
        emails = [row[0] for row in result]  # Extract emails into a list which is used to extract the first column (row[0]) from each row in the result of an SQL query.
    return emails if emails else ["No emails are found/fetched"]

# Fetch emails for dropdown list
email_options = hang()

search1 = st.selectbox("Choose an e-mail to find record", ["Choose One"] + email_options, key="search2")

# 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:
        submit1= st.form_submit_button("Submit")
    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("Search")

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.