table.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. import streamlit as st
  2. import pandas as pd
  3. import plotly.graph_objects as go
  4. def scrollable_dataframe_1():
  5. st.title("Scrollable DataFrame Example")
  6. # 创建一个宽表格以测试滚动功能
  7. df = pd.DataFrame(
  8. {
  9. "1 column": [1, 2, 3, 4, 5, 6, 7],
  10. "2 column": [10, 20, 30, 40, 50, 60, 70],
  11. "3 column": [1, 2, 3, 4, 5, 6, 7],
  12. "4 column": [10, 20, 30, 40, 50, 60, 70],
  13. "5 column": [1, 2, 3, 4, 5, 6, 7],
  14. "6 column": [10, 20, 30, 40, 50, 60, 70],
  15. "7 column": [1, 2, 3, 4, 5, 6, 7],
  16. "8 column": [10, 20, 30, 40, 50, 60, 70],
  17. "9 column": [1, 2, 3, 4, 5, 6, 7],
  18. "10 column": [10, 20, 30, 40, 50, 60, 70],
  19. "11 column": [1, 2, 3, 4, 5, 6, 7],
  20. "12 column": [10, 20, 30, 40, 50, 60, 70],
  21. }
  22. )
  23. st.write("1 + 1 = ", 2)
  24. st.write("Below is a DataFrame:", df, "Above is a dataframe.")
  25. st.dataframe(df, width='content', height=100)
  26. st.latex(r'''
  27. a + ar + a r^2 + a r^3 + \cdots + a r^{n-1} =
  28. \sum_{k=0}^{n-1} ar^k =
  29. a \left(\frac{1-r^{n}}{1-r}\right)
  30. ''')
  31. st.help(st.latex)
  32. def scrollable_dataframe_2():
  33. st.title("Scrollable DataFrame with Custom CSS")
  34. # 大数据集
  35. df = pd.DataFrame({
  36. f"column_{i}": [f"data_{j}_{i}" for j in range(100)]
  37. for i in range(20)
  38. })
  39. st.write("1 + 1 = ", 2)
  40. # 自定义CSS样式
  41. st.markdown("""
  42. <style>
  43. .custom-dataframe-container {
  44. height: 400px;
  45. width: 100%;
  46. max-width: 1000px;
  47. overflow: auto;
  48. border: 2px solid #4CAF50;
  49. border-radius: 10px;
  50. padding: 15px;
  51. margin: 10px 0;
  52. background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  53. box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
  54. }
  55. .custom-dataframe-container::-webkit-scrollbar {
  56. width: 12px;
  57. height: 12px;
  58. }
  59. .custom-dataframe-container::-webkit-scrollbar-track {
  60. background: #f1f1f1;
  61. border-radius: 10px;
  62. }
  63. .custom-dataframe-container::-webkit-scrollbar-thumb {
  64. background: #888;
  65. border-radius: 10px;
  66. }
  67. .custom-dataframe-container::-webkit-scrollbar-thumb:hover {
  68. background: #555;
  69. }
  70. </style>
  71. """, unsafe_allow_html=True)
  72. # 应用自定义样式
  73. st.markdown("Below is a DataFrame:")
  74. st.markdown('<div class="custom-dataframe-container">', unsafe_allow_html=True)
  75. # 使用st.dataframe,它会继承容器的滚动设置
  76. # st.dataframe(df, use_container_width=True)
  77. st.dataframe(df, width='content')
  78. st.markdown('</div>', unsafe_allow_html=True)
  79. st.write("Above is a dataframe.")
  80. def scrollable_dataframe_2_fixed():
  81. st.title("Scrollable DataFrame - Fixed Version")
  82. # 大数据集
  83. df = pd.DataFrame({
  84. f"column_{i}": [f"data_{j}_{i}" for j in range(100)]
  85. for i in range(20)
  86. })
  87. st.write("1 + 1 = ", 2)
  88. st.markdown("Below is a DataFrame:")
  89. # 方案1:直接使用st.dataframe,不包装容器
  90. st.dataframe(
  91. df,
  92. height=400,
  93. width=1000,
  94. )
  95. st.write("Above is a dataframe.")
  96. def scrollable_dataframe_3_plotly():
  97. st.title("Scrollable DataFrame with Plotly")
  98. # 大数据集
  99. df = pd.DataFrame({
  100. f"column_{i}": [f"data_{j}_{i}" for j in range(100)]
  101. for i in range(15)
  102. })
  103. st.write("1 + 1 = ", 2)
  104. st.markdown("Below is a DataFrame:")
  105. # 使用Plotly表格
  106. fig = go.Figure(data=[go.Table(
  107. header=dict(
  108. values=list(df.columns),
  109. fill_color='paleturquoise',
  110. align='left',
  111. font=dict(size=12)
  112. ),
  113. cells=dict(
  114. values=[df[col] for col in df.columns],
  115. fill_color='lavender',
  116. align='left',
  117. font=dict(size=11)
  118. )
  119. )])
  120. fig.update_layout(
  121. width=1000,
  122. height=400,
  123. margin=dict(l=0, r=0, t=0, b=0)
  124. )
  125. # st.plotly_chart(fig, use_container_width=True)
  126. st.plotly_chart(fig, width='stretch')
  127. st.write("Above is a dataframe.")
  128. # 页面选择
  129. page = st.radio("Select Page", [
  130. "Scrollable DataFrame 1",
  131. "Scrollable with Custom CSS",
  132. "Scrollable with Custom CSS Fixed Version",
  133. "Plotly Version",
  134. ])
  135. if page == "Scrollable DataFrame 1":
  136. scrollable_dataframe_1()
  137. elif page == "Scrollable with Custom CSS":
  138. scrollable_dataframe_2()
  139. elif page == "Scrollable with Custom CSS Fixed Version":
  140. scrollable_dataframe_2_fixed()
  141. elif page == "Plotly Version":
  142. scrollable_dataframe_3_plotly()