CoolFace
Apppublic

Naruto9/SAAS-Automation.demo

sourceHugging Facemitupdated 2y agoView on Hugging Face
0likes
app.py196 linesDownload Raw Back to root
1import streamlit as st2import pandas as pd3import sqlite34 5# Establish database connection (create if it doesn't exist)6conn = sqlite3.connect('dispatch_data.db')7cursor = conn.cursor()8 9# Create tables if they don't exist10cursor.execute('''11    CREATE TABLE IF NOT EXISTS drivers (12        driver_id TEXT PRIMARY KEY,13        name TEXT,14        location TEXT,15        status TEXT16    )17''')18cursor.execute('''19    CREATE TABLE IF NOT EXISTS orders (20        order_id TEXT PRIMARY KEY,21        pickup_location TEXT,22        dropoff_location TEXT,23        status TEXT24    )25''')26cursor.execute('''27    CREATE TABLE IF NOT EXISTS zone_pressure (28        zone_id TEXT PRIMARY KEY,29        pressure_level INTEGER30    )31''')32cursor.execute('''33    CREATE TABLE IF NOT EXISTS analytics (34        id INTEGER PRIMARY KEY AUTOINCREMENT,35        performance_indicators TEXT,36        driver_trips TEXT,37        delivery_times TEXT,38        delivery_delay TEXT,39        customer_satisfaction TEXT40    )41''')42 43# Functions to interact with the database44 45def fetch_data(table_name):46    cursor.execute(f"SELECT * FROM {table_name}")47    data = cursor.fetchall()48    columns = [description[0] for description in cursor.description]49    return pd.DataFrame(data, columns=columns)50 51def insert_data(table_name, data):52    columns = ', '.join(data.keys())53    placeholders = ', '.join(['?'] * len(data))54    query = f"INSERT INTO {table_name} ({columns}) VALUES ({placeholders})"55    cursor.execute(query, tuple(data.values()))56    conn.commit()57 58# DataFrames to store information59drivers = pd.DataFrame(columns=["driver_id", "name", "location", "status"])60orders = pd.DataFrame(columns=["order_id", "order_time", "pickup_location", "dropoff_location", "status"])61zone_pressure = pd.DataFrame(columns=['zone_id', 'pressure_level'])62analytics = pd.DataFrame(columns=['performance_indicators', 'driver_trips', 'delivery_times', 'delivery_delay', 'customer_satisfaction'])63 64# Basic UI structure65st.title("Dispatch Call Scheduler")66 67# Sidebar for navigation and actions68with st.sidebar:69    st.header("Navigation")70    selected_page = st.radio("Go to", ["Order Management", "Driver Management", "Zone Pressure", "Analytics"])71 72    st.header("Actions")73    if st.button("Schedule Dispatch"):74        # Logic to schedule a dispatch based on current data75        st.write("Dispatch scheduled!")76 77# Order Management page78if selected_page == "Order Management":79    st.subheader("Order Management")80 81    # Add new order82    with st.form("add_order_form"):83        st.write("Add New Order")84        order_id = st.text_input("Order ID")85        order_time = st.text_input("Order Time")86        pickup_location = st.text_input("Pickup Location")87        dropoff_location = st.text_input("Dropoff Location")88        status = st.selectbox("Status", ["Pending", "In Progress", "Completed"])89        submitted = st.form_submit_button("Add Order")90        if submitted:91            new_order = pd.DataFrame({92                'order_id': [order_id],93                'order_time': [order_time],94                'pickup_location': [pickup_location],95                'dropoff_location': [dropoff_location],96                'status': [status]97            })98            orders = pd.concat([orders, new_order], ignore_index=True)99            # Insert into the database100            insert_data('orders', new_order.to_dict(orient='records')[0])101            # Re-fetch data to reflect the changes102            orders = fetch_data('orders')103 104    # Display order list105    st.write(orders)106 107# Driver Management page108elif selected_page == "Driver Management":109    st.subheader("Driver Management")110 111    # Add new driver112    with st.form("add_driver_form"):113        st.write("Add New Driver")114        driver_id = st.text_input("Driver ID")115        name = st.text_input("Name")116        location = st.text_input("Location")117        status = st.selectbox("Status", ["Available", "Unavailable"])118        submitted = st.form_submit_button("Add Driver")119        if submitted:120            new_driver = pd.DataFrame({121                'driver_id': [driver_id],122                'name': [name],123                'location': [location],124                'status': [status]125            })126            drivers = pd.concat([drivers, new_driver], ignore_index=True)127            # Insert into the database128            insert_data('drivers', new_driver.to_dict(orient='records')[0])129            # Re-fetch data to reflect the changes130            drivers = fetch_data('drivers')131 132    # Display driver list133    st.write(drivers)134 135# Zone Monitoring page136elif selected_page == "Zone Pressure":137    st.subheader("Dynamic Zone Pressure Monitoring")138 139    # Add new zone pressure data140    with st.form("add_zone_pressure_form"):141        st.write("Add Zone Pressure Data")142        zone_id = st.text_input("Zone ID")143        pressure_level = st.number_input("Pressure Level", min_value=0, max_value=10, value=0)144        submitted = st.form_submit_button("Add Zone Data")145        if submitted:146            new_zone_data = pd.DataFrame({147                'zone_id': [zone_id],148                'pressure_level': [pressure_level]149            })150            zone_pressure = pd.concat([zone_pressure, new_zone_data], ignore_index=True)151            # Insert into the database152            insert_data('zone_pressure', new_zone_data.to_dict(orient='records')[0])153            # Re-fetch data to reflect the changes154            zone_pressure = fetch_data('zone_pressure')155 156    # Display zone pressure data157    st.write(zone_pressure)158 159# Analytics Dashboard page160elif selected_page == "Analytics":161    st.subheader("Analytics Dashboard")162 163    # Add analytics information (consider using a more structured input method)164    with st.form("add_analytics_form"):165        st.write("Add Analytics Information")166        performance_indicators = st.text_input("Performance Indicators")167        driver_trips = st.text_input("Driver Trips")168        delivery_times = st.text_input("Delivery Times")169        delivery_delay = st.text_input("Delivery Delay")170        customer_satisfaction = st.text_input("Customer Satisfaction")171        submitted = st.form_submit_button("Add Analytics Data")172        if submitted:173            new_analytics_data = pd.DataFrame({174                'performance_indicators': [performance_indicators],175                'driver_trips': [driver_trips],176                'delivery_times': [delivery_times],177                'delivery_delay': [delivery_delay],178                'customer_satisfaction': [customer_satisfaction]179            })180            analytics = pd.concat([analytics, new_analytics_data], ignore_index=True)181            # Insert into the database182            insert_data('analytics', new_analytics_data.to_dict(orient='records')[0])183            # Re-fetch data to reflect the changes184            analytics = fetch_data('analytics')185 186    # Display analytics information187    st.write(analytics)188 189# Fetch initial data from the database190drivers = fetch_data('drivers')191orders = fetch_data('orders')192zone_pressure = fetch_data('zone_pressure')193analytics = fetch_data('analytics')194 195# Close the database connection when the app is done196st.session_state.on_session_end = conn.close