1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
| import numpy as np import pandas as pd import altair as alt import pydeck as pdk from PIL import Image import streamlit as st import plotly.express as px from graphviz import Digraph import matplotlib.pyplot as plt from bokeh.plotting import figure
st.title("Streamlit显示")
st.sidebar.title("显示项目")
category = st.sidebar.selectbox("", ["文本", "数据", "媒体", "图表"])
if category == "文本": if st.sidebar.button("❀ 标题"):
st.header("较小标题")
st.subheader("更小标题")
st.caption("最小标题")
st.write("🌹 这里可以放置与“标题”相关的信息和数据。")
elif st.sidebar.button("❀ Text"): st.subheader("Text")
st.text(""" 人们并非生来平等,人的善恶感也生来各异。——《了不起的盖茨比》 """)
st.write("🌹 这里可以放置与“Text”相关的信息和数据。")
elif st.sidebar.button("❀ Json"): st.subheader("Json")
st.json({ "id": 1, "username": "钱三两", "address": { "street": "梨花巷", "city": "庐州" } })
st.write("🌹 这里可以放置与“Json”相关的信息和数据。")
elif st.sidebar.button("❀ Code"): st.subheader("Python")
st.code( """ import importlib.metadata
for dist in importlib.metadata.distributions(): try: print(f"Package: {dist.name}") print(f"Location: {dist.locate_file('')}") print("---") except FileNotFoundError: print( f"Package: {dist.name} - " + f"Location not found (likely built-in or stdlib)" ) print("---") except Exception as e: print(f"Package: {dist.name} - Error: {e}") print("---") """, language="python" )
st.write("🌹 这里可以放置与“Code”相关的信息和数据。")
elif st.sidebar.button("❀ LaTeX"): st.subheader("LaTeX")
st.latex( r""" a + ar + a r ^ 2 + a r ^ 3 + \cdots + a r ^ {n - 1} = \sum_{k = 0} ^ {n - 1} ar ^ k = a \left(\frac{1 - r ^ {n}}{1 - r} \right) """ )
st.write("🌹 这里可以放置与“LaTeX”相关的信息和数据。")
elif st.sidebar.button("❀ Markdown"): st.subheader("Markdown")
st.markdown(""" ###### 北回归线 现在我住在波勒兹别墅,这里找不到一点儿灰尘, 也没有一件东西摆得不是地方, 除了我们,这里再没有别人,我们死了。 ###### 受戒 人间存一角,聊放侧枝花。欣然亦自得,不共赤城霞。 """)
st.write("🌹 Markdown格式文本,使用两个空格实现硬换行。") st.write("🌹 这里可以放置与“Markdown”相关的信息和数据。")
elif category == "数据": if st.sidebar.button("☆ Table"): st.subheader("Table")
df = pd.DataFrame( np.random.randn(10, 5), columns=["col %d" % i for i in range(5)] )
st.table(df)
st.write("🌹 这里可以放置与“Table”相关的信息和数据。")
elif st.sidebar.button("☆ Metric"): st.subheader("Metric")
col1, col2, col3 = st.columns(3) col1.metric("Temperature", "70 °F", "1.2 °F") col2.metric("Wind", "9 mph", "-8%") col3.metric("Humidity", "86%", "4%")
st.write("🌹 这里可以放置与“Metric”相关的信息和数据。")
elif st.sidebar.button("☆ Dataframe"): st.subheader("Dataframe")
random_data = np.random.rand(7, 10) df = pd.DataFrame( random_data, columns=[f"Col{i}" for i in range(1, 11)] )
st.dataframe(df)
st.write("🌹 这里可以放置与“Dataframe”相关的信息和数据。")
elif category == "媒体": if st.sidebar.button("❄ 视频"): st.subheader("本地视频")
video_file = open(r"./file/show.mp4", "rb") video_bytes = video_file.read()
st.video( video_bytes, format="mp4", start_time=1 )
st.subheader("网络视频")
st.video( "https://videos.pexels.com/video-files" "/4376249/4376249-hd_1920_1080_24fps.mp4" )
st.write("🌹 这里可以放置与“视频”相关的信息和数据。")
elif st.sidebar.button("❄ 图片"): st.subheader("本地图片")
img = Image.open("./file/show.jpg")
st.image(image=img, width=700)
st.subheader("网络图片")
st.image( "https://img2.baidu.com/it/u=3645742772,1483764043" "&fm=253&fmt=auto&app=120&f=JPEG?w=1422&h=800", width=700 )
st.write("🌹 这里可以放置与“图片”相关的信息和数据。")
elif st.sidebar.button("❄ 音频"): st.subheader("本地音频:《同簪》")
audio_file = open("./file/show.mp3", "rb") audio_bytes = audio_file.read() st.audio(audio_bytes, format="audio/mp3")
st.subheader("网络音频:《Fresh Focus》")
st.audio( "https://freepd.com/music/Fresh%20Focus.mp3" )
st.write("🌹 这里可以放置与“音频”相关的信息和数据。")
elif category == "图表": if st.sidebar.button("✦ Map"): st.subheader("Map")
data = { "latitude": [24.435028, 24.537242, 24.679506], "longitude": [118.089404, 118.022565, 118.174976], "name": [ "厦门市思明区演武西路世茂海峡大厦", "厦门市马銮湾环湾带状公园", "厦门方特梦幻王国" ] }
st.map(data, zoom=10, use_container_width=True)
st.write("🌹 这里可以放置与“Map”相关的信息和数据。")
elif st.sidebar.button("✦ PyPlot"): st.subheader("PyPlot")
m = 20
n = 256 * m + 1
tx = np.linspace(0, 24 * np.pi, n)
x = np.zeros(n) y = np.zeros(n)
for i in range(n): t = tx[i] x[i] = ( np.sin(t) * (np.exp(np.cos(t)) - 2 * np.cos(4 * t) - (np.sin(t / 12)) ** 5) ) y[i] = ( np.cos(t) * (np.exp(np.cos(t)) - 2 * np.cos(4 * t) - (np.sin(t / 12)) ** 5) )
fig = plt.figure(figsize=(8, 6), dpi=100) plt.plot(x, y, color="orange")
st.pyplot(fig)
st.write("🌹 这里可以放置与“PyPlot”相关的信息和数据。")
elif st.sidebar.button("✦ Bar_Chart"): st.subheader("Bar_Chart")
data = pd.DataFrame( np.random.randn(20, 3), columns=["a", "b", "c"] ) st.bar_chart(data)
st.write("🌹 这里可以放置与“Bar_Chart”相关的信息和数据。")
elif st.sidebar.button("✦ Line_Chart"): st.subheader("Line_Chart")
data = pd.DataFrame( np.random.randn(20, 3), columns=["a", "b", "c"] ) st.line_chart(data)
st.write("🌹 这里可以放置与“Line_Chart”相关的信息和数据。")
elif st.sidebar.button("✦ Area_Chart"): st.subheader("Area_Chart")
data = pd.DataFrame( np.random.randn(20, 3), columns=["a", "b", "c"] ) st.area_chart(data)
st.write("🌹 这里可以放置与“Area_Chart”相关的信息和数据。")
elif st.sidebar.button("✦ Altair_Chart"): st.subheader("Altair_Chart")
data = pd.DataFrame( np.random.randn(20, 3), columns=["a", "b", "c"] ) c = alt.Chart(data).mark_circle().encode( x="a", y="b", size="c", color="c", tooltip=["a", "b", "c"] ) st.altair_chart(c, use_container_width=True)
st.write("🌹 这里可以放置与“Altair_Chart”相关的信息和数据。")
elif st.sidebar.button("✦ Plotly_Chart"): st.subheader("Plotly_Chart")
df = px.data.iris() fig = px.scatter( df, x="sepal_width", y="sepal_length", color="species" ) st.plotly_chart(fig)
st.write("🌹 这里可以放置与“Plotly_Chart”相关的信息和数据。")
elif st.sidebar.button("✦ Bokeh_Chart"): st.subheader("Bokeh_Chart")
x = np.linspace(0, 10, 100) y = np.sin(x) p = figure(title="Bokeh图表") p.line(x, y, legend_label="正弦波", line_width=2) st.bokeh_chart(p)
st.write("🌹 这里可以放置与“Bokeh_Chart”相关的信息和数据。")
elif st.sidebar.button("✦ PyDeck_Chart"): st.subheader("PyDeck_Chart")
data = pd.DataFrame( np.random.randn(1000, 2) / [50, 50] + [37.76, -122.4], columns=["lat", "lon"] )
st.pydeck_chart( pdk.Deck( map_style=None, initial_view_state=pdk.ViewState( latitude=37.76, longitude=-122.4, zoom=11, pitch=50, ), layers=[ pdk.Layer( "HexagonLayer", data=data, elevation_range=[0, 1000], radius=200, elevation_scale=4, pickable=True, extruded=True, get_position="[lon, lat]", ), pdk.Layer( "ScatterplotLayer", data=data, get_radius=200, get_color="[200, 30, 0, 160]", get_position="[lon, lat]", ), ], ) )
st.write("🌹 这里可以放置与“PyDeck_Chart”相关的信息和数据。")
elif st.sidebar.button("✦ Graphviz_Chart"): st.subheader("Graphviz_Chart")
dot = Digraph() dot.node("A", "节点 A") dot.node("B", "节点 B") dot.node("C", "节点 C") dot.node("D", "节点 D") dot.node("E", "节点 E") dot.node("F", "节点 F") dot.edges(["AB", "AC", "AD", "CE", "CF"]) st.graphviz_chart(dot)
st.write("🌹 这里可以放置与“Graphviz_Chart”相关的信息和数据。")
elif st.sidebar.button("✦ Vega_Lite_Chart"): st.subheader("Vega_Lite_Chart")
x = np.linspace(0, 10, 100) y = np.sin(x) df = pd.DataFrame({"x": x, "y": y}) st.vega_lite_chart(df, { "mark": "line", "encoding": { "x": {"field": "x", "type": "quantitative"}, "y": {"field": "y", "type": "quantitative"} } })
st.write("🌹 这里可以放置与“Vega_Lite_Chart”相关的信息和数据。")
st.write( "🌸 这里是主区域," "点击左侧项目进行查看后,按下R键可返回首页。" )
|