dashboard.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import streamlit as st
  2. import pandas as pd
  3. import numpy as np
  4. import plotly.express as px
  5. from datetime import datetime, timedelta
  6. st.title("📊 Dashboard")
  7. st.markdown("欢迎来到系统仪表板")
  8. # 模拟数据
  9. @st.cache_data
  10. def load_dashboard_data():
  11. dates = pd.date_range(start=datetime.now() - timedelta(days=30), periods=30)
  12. data = {
  13. 'date': dates,
  14. 'users': np.random.randint(100, 1000, 30),
  15. 'revenue': np.random.uniform(1000, 5000, 30),
  16. 'errors': np.random.randint(0, 50, 30)
  17. }
  18. return pd.DataFrame(data)
  19. # 加载数据
  20. df = load_dashboard_data()
  21. # 关键指标
  22. col1, col2, col3, col4 = st.columns(4)
  23. with col1:
  24. st.metric(
  25. label="总用户数",
  26. value=f"{df['users'].sum():,}",
  27. delta=f"{df['users'].iloc[-1] - df['users'].iloc[-2]:+d}"
  28. )
  29. with col2:
  30. st.metric(
  31. label="总收入",
  32. value=f"${df['revenue'].sum():,.2f}",
  33. delta=f"${df['revenue'].iloc[-1] - df['revenue'].iloc[-2]:+.2f}"
  34. )
  35. with col3:
  36. st.metric(
  37. label="平均错误数",
  38. value=f"{df['errors'].mean():.1f}",
  39. delta=f"{df['errors'].iloc[-1] - df['errors'].mean():.1f}"
  40. )
  41. with col4:
  42. st.metric(
  43. label="系统状态",
  44. value="正常",
  45. delta="99.9% 正常运行时间"
  46. )
  47. # 图表
  48. st.subheader("📈 趋势分析")
  49. tab1, tab2, tab3 = st.tabs(["用户趋势", "收入趋势", "错误趋势"])
  50. with tab1:
  51. fig_users = px.line(df, x='date', y='users', title='用户数量趋势')
  52. st.plotly_chart(fig_users, use_container_width=True)
  53. with tab2:
  54. fig_revenue = px.bar(df, x='date', y='revenue', title='收入趋势')
  55. st.plotly_chart(fig_revenue, use_container_width=True)
  56. with tab3:
  57. fig_errors = px.scatter(df, x='date', y='errors', title='错误数量趋势')
  58. st.plotly_chart(fig_errors, use_container_width=True)
  59. # 数据表格
  60. st.subheader("📋 详细数据")
  61. st.dataframe(df, use_container_width=True)