Create an app with: (1) sidebar that's collapsible. User should be able to log in here with a username and a password. (2) Tab #1. It should load in a sample dataset (.csv), then visualize it using a bar shart. (3) Tab #2. It should have a chat where the user can interact with the data via chat.
Drop files here
or click to upload
import streamlit as st
import pandas as pd
import plotly.express as px
import time
import random
# Initialize session state variables
if 'authenticated' not in st.session_state:
st.session_state.authenticated = False
if 'messages' not in st.session_state:
st.session_state.messages = []
if 'df' not in st.session_state:
# Sample dataset
st.session_state.df = pd.DataFrame({
'Category': ['A', 'B', 'C', 'D', 'E'],
'Values': [23, 45, 56, 78, 90]
})
# Configure page
st.set_page_config(layout="wide")
# Sidebar with authentication
with st.sidebar:
st.title("Login")
with st.expander("Click to expand", expanded=not st.session_state.authenticated):
if not st.session_state.authenticated:
username = st.text_input("Username")
password = st.text_input("Password", type="password")
if st.button("Login"):
# Simple authentication - in real app, use secure methods
if username == "admin" and password == "password":
st.session_state.authenticated = True
st.success("Logged in successfully!")
st.rerun()
else:
st.error("Invalid credentials")
else:
st.success("Logged in as admin")
if st.button("Logout"):
st.session_state.authenticated = False
st.rerun()
# Main content
if st.session_state.authenticated:
tab1, tab2 = st.tabs(["Data Visualization", "Chat Interface"])
with tab1:
st.title("Data Visualization")
# Display data
st.subheader("Sample Dataset")
st.dataframe(st.session_state.df)
# Create bar chart
st.subheader("Bar Chart")
fig = px.bar(st.session_state.df, x='Category', y='Values',
title='Sample Data Visualization')
st.plotly_chart(fig, use_container_width=True)
with tab2:
st.title("Chat Interface")
# 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
st.session_state.messages.append({"role": "user", "content": prompt})
# Display user message
with st.chat_message("user"):
st.write(prompt)
# Generate and display response
with st.chat_message("assistant"):
response = f"I understand you're asking about the data. The dataset contains {len(st.session_state.df)} rows with categories and their corresponding values. What specific information would you like to know?"
st.write_stream(response)
# Add assistant response to history
st.session_state.messages.append({"role": "assistant", "content": response})
else:
st.warning("Please log in to access the application")
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?