container.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. import streamlit as st
  2. def container_default():
  3. st.title("📦 默认Container - 只有垂直滚动")
  4. long_text = "Lorem ipsum. 12345678901234567890123456789012345678901234567890123456789012345678901234567890" * 1000
  5. with st.container(height=300, width=600, border=True):
  6. st.markdown(long_text)
  7. st.write("☝️ 上面的容器只能垂直滚动,长文本会自动换行")
  8. def container_with_horizontal_scroll():
  9. st.title("↔️ 使用CSS实现横向滚动")
  10. # 方案1:真正的双向滚动(修复版)
  11. st.subheader("🔄 双向滚动 - 修复版")
  12. # 创建既宽又高的内容
  13. long_text = "Lorem ipsum. 12345678901234567890123456789012345678901234567890123456789012345678901234567890" * 1000
  14. wide_content = """
  15. <div style="height: 300px; width: 600px; overflow: auto; border: 2px solid #4CAF50; border-radius: 10px; padding: 15px; background-color: #f9f9f9;">
  16. <div style="min-width: 1200px; min-height: 800px;">
  17. <h3>这个容器支持横向和纵向滚动</h3>
  18. <p>横向内容:AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA</p>
  19. <p>正常换行内容:Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
  20. <table style="border-collapse: collapse; margin: 20px 0; min-width: 1000px;">
  21. <tr style="background-color: #e6f3ff;">
  22. <th style="border: 1px solid #ddd; padding: 8px;">超长列名1超长列名1超长列名1</th>
  23. <th style="border: 1px solid #ddd; padding: 8px;">超长列名2超长列名2超长列名2</th>
  24. <th style="border: 1px solid #ddd; padding: 8px;">超长列名3超长列名3超长列名3</th>
  25. <th style="border: 1px solid #ddd; padding: 8px;">超长列名4超长列名4超长列名4</th>
  26. <th style="border: 1px solid #ddd; padding: 8px;">超长列名5超长列名5超长列名5</th>
  27. </tr>
  28. """
  29. # 添加多行表格数据
  30. for i in range(50):
  31. wide_content += f"""
  32. <tr>
  33. <td style="border: 1px solid #ddd; padding: 8px;">数据{i}-1数据{i}-1数据{i}-1</td>
  34. <td style="border: 1px solid #ddd; padding: 8px;">数据{i}-2数据{i}-2数据{i}-2</td>
  35. <td style="border: 1px solid #ddd; padding: 8px;">数据{i}-3数据{i}-3数据{i}-3</td>
  36. <td style="border: 1px solid #ddd; padding: 8px;">数据{i}-4数据{i}-4数据{i}-4</td>
  37. <td style="border: 1px solid #ddd; padding: 8px;">数据{i}-5数据{i}-5数据{i}-5</td>
  38. </tr>
  39. """
  40. wide_content += """
  41. </table>
  42. <p>更多垂直内容...</p>
  43. <div style="height: 200px; background: linear-gradient(45deg, #ff9a9e 0%, #fecfef 50%, #fecfef 100%); border-radius: 10px; margin: 20px 0; display: flex; align-items: center; justify-content: center;">
  44. <h2>这是一个高度为200px的彩色区域</h2>
  45. </div>
  46. </div>
  47. </div>
  48. """
  49. # 添加滚动条样式
  50. st.markdown("""
  51. <style>
  52. .dual-scroll {
  53. height: 300px;
  54. width: 600px;
  55. overflow: auto;
  56. border: 2px solid #4CAF50;
  57. border-radius: 10px;
  58. padding: 15px;
  59. background-color: #f9f9f9;
  60. }
  61. .dual-scroll::-webkit-scrollbar {
  62. width: 12px;
  63. height: 12px;
  64. }
  65. .dual-scroll::-webkit-scrollbar-track {
  66. background: #f1f1f1;
  67. border-radius: 10px;
  68. }
  69. .dual-scroll::-webkit-scrollbar-thumb {
  70. background: #888;
  71. border-radius: 10px;
  72. }
  73. .dual-scroll::-webkit-scrollbar-thumb:hover {
  74. background: #555;
  75. }
  76. .dual-scroll::-webkit-scrollbar-corner {
  77. background: #f1f1f1;
  78. }
  79. </style>
  80. """, unsafe_allow_html=True)
  81. st.markdown(wide_content, unsafe_allow_html=True)
  82. st.write("☝️ 上面的容器支持真正的横向和纵向双向滚动")
  83. # 方案2:只有横向滚动的版本(原来的)
  84. st.subheader("↔️ 只有横向滚动版本")
  85. long_text_no_wrap = "Lorem ipsum dolor sit amet consectetur adipiscing elit " * 50
  86. st.markdown("""
  87. <style>
  88. .horizontal-only {
  89. height: 100px;
  90. width: 600px;
  91. overflow-x: auto;
  92. overflow-y: hidden;
  93. border: 2px solid #FF6B6B;
  94. border-radius: 10px;
  95. padding: 15px;
  96. background-color: #fff5f5;
  97. white-space: nowrap;
  98. }
  99. .horizontal-only::-webkit-scrollbar {
  100. height: 12px;
  101. }
  102. .horizontal-only::-webkit-scrollbar-track {
  103. background: #f1f1f1;
  104. border-radius: 10px;
  105. }
  106. .horizontal-only::-webkit-scrollbar-thumb {
  107. background: #ff6b6b;
  108. border-radius: 10px;
  109. }
  110. </style>
  111. """, unsafe_allow_html=True)
  112. st.markdown(f'<div class="horizontal-only">{long_text_no_wrap}</div>', unsafe_allow_html=True)
  113. st.write("☝️ 上面的容器只支持横向滚动")
  114. # 方案3:垂直滚动版本
  115. st.subheader("↕️ 只有垂直滚动版本")
  116. vertical_content = """
  117. <div style="height: 200px; width: 400px; overflow-y: auto; overflow-x: hidden; border: 2px solid #9B59B6; border-radius: 10px; padding: 15px; background-color: #f8f4ff;">
  118. """
  119. for i in range(20):
  120. vertical_content += f"<p>这是第{i+1}行文本内容,用于测试垂直滚动功能。</p>"
  121. vertical_content += "</div>"
  122. st.markdown(vertical_content, unsafe_allow_html=True)
  123. st.write("☝️ 上面的容器只支持垂直滚动")
  124. def container_code_block():
  125. st.title("🔤 代码块容器 - 自带横向滚动")
  126. # 长代码行
  127. long_code = """
  128. def very_long_function_name_that_exceeds_normal_width():
  129. very_long_variable_name = "this is a very long string that will definitely exceed the container width and require horizontal scrolling to view completely"
  130. another_very_long_variable = {"key1": "very_long_value_1", "key2": "very_long_value_2", "key3": "very_long_value_3"}
  131. return very_long_variable_name + str(another_very_long_variable)
  132. # 更多长代码行
  133. print("This is a very long print statement that contains lots of text and will definitely require horizontal scrolling to see the complete content")
  134. """
  135. with st.container(height=300, border=True):
  136. st.code(long_code, language="python")
  137. st.write("☝️ 代码块自动支持横向滚动")
  138. def container_dataframe():
  139. st.title("📊 DataFrame容器 - 自带横向滚动")
  140. import pandas as pd
  141. # 创建宽DataFrame
  142. wide_df = pd.DataFrame({
  143. f"很长的列名_{i}": [f"很长的数据内容_{j}_{i}" for j in range(20)]
  144. for i in range(15)
  145. })
  146. with st.container(height=300, border=True):
  147. st.dataframe(wide_df, width='stretch')
  148. st.write("☝️ DataFrame自动支持横向滚动")
  149. def container_custom_html():
  150. st.title("🎨 自定义HTML容器")
  151. # 创建表格数据
  152. table_html = """
  153. <div style="height: 300px; width: 600px; overflow: auto; border: 2px solid #FF6B6B; border-radius: 10px; padding: 10px;">
  154. <table style="min-width: 1200px; border-collapse: collapse;">
  155. <tr style="background-color: #f2f2f2;">
  156. <th style="border: 1px solid #ddd; padding: 8px; white-space: nowrap;">Very Long Column Header 1</th>
  157. <th style="border: 1px solid #ddd; padding: 8px; white-space: nowrap;">Very Long Column Header 2</th>
  158. <th style="border: 1px solid #ddd; padding: 8px; white-space: nowrap;">Very Long Column Header 3</th>
  159. <th style="border: 1px solid #ddd; padding: 8px; white-space: nowrap;">Very Long Column Header 4</th>
  160. <th style="border: 1px solid #ddd; padding: 8px; white-space: nowrap;">Very Long Column Header 5</th>
  161. <th style="border: 1px solid #ddd; padding: 8px; white-space: nowrap;">Very Long Column Header 6</th>
  162. </tr>
  163. """
  164. # 添加表格行
  165. for i in range(20):
  166. table_html += f"""
  167. <tr>
  168. <td style="border: 1px solid #ddd; padding: 8px; white-space: nowrap;">Very long data content {i}-1</td>
  169. <td style="border: 1px solid #ddd; padding: 8px; white-space: nowrap;">Very long data content {i}-2</td>
  170. <td style="border: 1px solid #ddd; padding: 8px; white-space: nowrap;">Very long data content {i}-3</td>
  171. <td style="border: 1px solid #ddd; padding: 8px; white-space: nowrap;">Very long data content {i}-4</td>
  172. <td style="border: 1px solid #ddd; padding: 8px; white-space: nowrap;">Very long data content {i}-5</td>
  173. <td style="border: 1px solid #ddd; padding: 8px; white-space: nowrap;">Very long data content {i}-6</td>
  174. </tr>
  175. """
  176. table_html += """
  177. </table>
  178. </div>
  179. """
  180. st.markdown(table_html, unsafe_allow_html=True)
  181. st.write("☝️ 自定义HTML表格支持横向和纵向滚动")
  182. def container_plotly_chart():
  183. st.title("📈 Plotly图表 - 自带横向滚动")
  184. import plotly.graph_objects as go
  185. import pandas as pd
  186. # 创建宽图表数据
  187. categories = [f"Category_{i}" for i in range(30)]
  188. values = [i * 10 + 50 for i in range(30)]
  189. fig = go.Figure(data=[
  190. go.Bar(x=categories, y=values, name="Long Bar Chart")
  191. ])
  192. fig.update_layout(
  193. title="Very Wide Bar Chart with Many Categories",
  194. width=1500, # 设置很宽的图表
  195. height=400,
  196. xaxis_title="Categories (Very Long Names)",
  197. yaxis_title="Values"
  198. )
  199. with st.container(height=450, border=True):
  200. st.plotly_chart(fig, width='content')
  201. st.write("☝️ Plotly图表在容器中自动支持横向滚动")
  202. def container_iframe():
  203. st.title("🌐 iframe容器")
  204. iframe_html = """
  205. <iframe
  206. src="https://www.openstreetmap.org/export/embed.html?bbox=-0.004017949104309082%2C51.47612752641776%2C0.00030577182769775%2C51.478569861898606&layer=mapnik"
  207. style="width: 800px; height: 300px; border: 2px solid #4CAF50; border-radius: 10px;"
  208. scrolling="yes">
  209. </iframe>
  210. """
  211. with st.container(height=350, border=True):
  212. st.markdown(iframe_html, unsafe_allow_html=True)
  213. st.write("☝️ iframe内容可以有自己的滚动条")
  214. # 页面选择
  215. page = st.radio("Select Container Type", [
  216. "📦 Default Container (Vertical Only)",
  217. "↔️ CSS Horizontal Scroll",
  218. "🔤 Code Block Container",
  219. "📊 DataFrame Container",
  220. "🎨 Custom HTML Container",
  221. "📈 Plotly Chart Container",
  222. "🌐 Iframe Container"
  223. ])
  224. if page == "📦 Default Container (Vertical Only)":
  225. container_default()
  226. elif page == "↔️ CSS Horizontal Scroll":
  227. container_with_horizontal_scroll()
  228. elif page == "🔤 Code Block Container":
  229. container_code_block()
  230. elif page == "📊 DataFrame Container":
  231. container_dataframe()
  232. elif page == "🎨 Custom HTML Container":
  233. container_custom_html()
  234. elif page == "📈 Plotly Chart Container":
  235. container_plotly_chart()
  236. elif page == "🌐 Iframe Container":
  237. container_iframe()