Create an app to visualize e-commerce orders
To upload files, please first save the app
import streamlit as st
import pandas as pd
import altair as alt
# Sample data
@st.cache_data
def load_data():
data = {
'order_id': [1, 2, 3, 4, 5],
'product': ['Widget A', 'Widget B', 'Widget A', 'Widget C', 'Widget B'],
'quantity': [5, 3, 2, 7, 1],
'price': [20, 30, 20, 50, 30],
'date': ['2023-01-01', '2023-01-03', '2023-01-04', '2023-01-05', '2023-01-02']
}
return pd.DataFrame(data)
st.title('E-commerce Orders Visualization')
# Load the data
orders_df = load_data()
# Display the orders data
st.subheader('Orders Data')
st.dataframe(orders_df)
# Create a summary chart
orders_summary = orders_df.groupby('product').agg(total_quantity=('quantity', 'sum'), total_sales=('price', 'sum')).reset_index()
# Create a bar chart using Altair
bar_chart = alt.Chart(orders_summary).mark_bar().encode(
x=alt.X('product:N', title='Product'),
y=alt.Y('total_quantity:Q', title='Total Quantity'),
tooltip=['product:N', 'total_quantity:Q', 'total_sales:Q']
).interactive()
st.subheader('Total Quantity Sold by Product')
st.altair_chart(bar_chart, use_container_width=True)
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?