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.
To upload files, please first save the app
import streamlit as st
import pandas as pd
import plotly.express as px
# Initialize session state
if "authenticated" not in st.session_state:
st.session_state.authenticated = False
if "messages" not in st.session_state:
st.session_state.messages = []
if "data" not in st.session_state:
# Load sample data
st.session_state.data = pd.read_csv("https://raw.githubusercontent.com/mwaskom/seaborn-data/master/tips.csv")
# Sidebar for authentication
with st.sidebar:
st.title("Login")
with st.expander("Enter credentials"):
username = st.text_input("Username")
password = st.text_input("Password", type="password")
if st.button("Login"):
# This is a simple authentication. In a real app, you'd want to use
# secure authentication methods
if username == "admin" and password == "admin":
st.session_state.authenticated = True
st.success("Logged in successfully!")
else:
st.error("Invalid credentials")
if st.session_state.authenticated:
# Main content area with tabs
tab1, tab2 = st.tabs(["Data Visualization", "Chat"])
with tab1:
st.title("Data Analysis Dashboard")
st.write("Sample data from tips dataset:")
st.dataframe(st.session_state.data.head())
# Create bar chart
fig = px.bar(
st.session_state.data,
x="day",
y="total_bill",
color="time",
title="Total Bill by Day and Time",
barmode="group"
)
st.plotly_chart(fig)
with tab2:
st.title("Chat with your Data")
# Display chat messages
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.write(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.write(prompt)
# Simulate assistant response
with st.chat_message("assistant"):
# Here you would typically integrate with a real LLM
response = "I can help you analyze the tips dataset. You can ask questions about total bills, tips, day of week, or time of day distributions."
st.write(response)
# Add assistant response to chat history
st.session_state.messages.append({"role": "assistant", "content": response})
else:
st.title("Welcome to Data Chat Dashboard")
st.write("Please login using the sidebar to access the dashboard.")
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?