Create an app with: (1) a collapsible sidebar. The user will log in here with the username and password. (2) Tab #1. The app should load some sample data (.csv) using pandas, then show a bar chart on the data. (3) Tab #2. It should have a chat, where the user can ask questions to a GPT model to interact with the sample data loaded in Tab #1.
Drop files here
or click to upload
import streamlit as st
import pandas as pd
import plotly.express as px
# Initialize session state for authentication
if 'authenticated' not in st.session_state:
st.session_state.authenticated = False
if 'messages' not in st.session_state:
st.session_state.messages = []
# Load sample data
@st.cache_data
def load_sample_data():
# Using a sample dataset about car sales
data = {
'manufacturer': ['Toyota', 'Honda', 'Ford', 'BMW', 'Mercedes'],
'sales': [150000, 130000, 120000, 80000, 90000],
'satisfaction': [4.5, 4.3, 4.1, 4.7, 4.6]
}
return pd.DataFrame(data)
def login():
# Hardcoded credentials for demo (in real app, use secure authentication)
valid_username = "demo"
valid_password = "password123"
with st.sidebar:
st.title("Login")
username = st.text_input("Username")
password = st.text_input("Password", type="password")
if st.button("Login"):
if username == valid_username and password == valid_password:
st.session_state.authenticated = True
st.success("Logged in successfully!")
st.rerun()
else:
st.error("Invalid credentials")
def main():
# Sidebar
with st.sidebar:
st.title("Navigation")
st.write(f"Welcome, User!")
if st.button("Logout"):
st.session_state.authenticated = False
st.rerun()
# Main content
st.title("Data Analytics Dashboard")
# Load data
df = load_sample_data()
# Create tabs
tab1, tab2 = st.tabs(["Data Visualization", "Chat Interface"])
with tab1:
st.header("Sales Analysis")
# Create bar chart using plotly
fig = px.bar(
df,
x='manufacturer',
y='sales',
title='Sales by Manufacturer',
labels={'manufacturer': 'Manufacturer', 'sales': 'Sales'},
color='satisfaction'
)
st.plotly_chart(fig)
# Show raw data
st.subheader("Raw Data")
st.dataframe(df)
with tab2:
st.header("Chat with Data Assistant")
# Display chat messages from history
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])
# Chat input
if prompt := st.chat_input("Ask about the data..."):
# Add user message to chat history
st.session_state.messages.append({"role": "user", "content": prompt})
# Display user message
with st.chat_message("user"):
st.markdown(prompt)
# Generate and display assistant response
response = f"I can help you analyze the data. For example, I can tell you that Toyota has the highest sales with {df['sales'].max():,} units."
with st.chat_message("assistant"):
st.markdown(response)
# Add assistant response to chat history
st.session_state.messages.append({"role": "assistant", "content": response})
# Main app logic
if not st.session_state.authenticated:
login()
else:
main()
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?