app.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. from datetime import datetime
  2. from numpy import empty, not_equal
  3. import streamlit as st
  4. import time
  5. import pandas as pd
  6. import numpy as np
  7. import streamlit.components.v1 as components
  8. #页面设置
  9. about_message = '''
  10. # About
  11. ## testing!
  12. :smile:
  13. '''
  14. st.set_page_config(
  15. page_title="Streamlit example",
  16. page_icon="./icon/android-chrome-192x192.png",
  17. layout="wide",
  18. initial_sidebar_state="collapsed",
  19. menu_items={
  20. 'Get Help': 'https://www.baidu.com/',
  21. 'Report a bug': None,
  22. 'About': about_message
  23. }
  24. )
  25. st.image("./icon/android-chrome-192x192.png")
  26. #t1函数,用于测试 on_change或 on_click
  27. def t1():
  28. st.text("t1-ing!")
  29. '''# 组件'''
  30. #勾选框
  31. a = st.checkbox('test_checkbox', value=False, key=None, help="testing", on_change=None, args=None, kwargs=None)
  32. #按钮
  33. b = st.button(label="button", key=None, help="testing!", on_click=None)
  34. #下载按钮
  35. c = st.download_button(label="download_button", data='testttt', file_name='test_.md', help='testing!', on_click=None)
  36. #单选框
  37. d = st.radio(label="What's your favorite movie genre",options=('Comedy', 'Drama', 'Documentary'),index=2, help='testing!')
  38. #下拉选项
  39. e = st.selectbox('slectbox',('Comedy', 'Drama', 'Documentary'),index=2, help='testing!')
  40. #多选
  41. f = st.multiselect('multiselect',('Comedy', 'Drama', 'Documentary'), default=['Drama'], help='testing!')
  42. #滑动条
  43. g = st.slider(label="slider", min_value=-10, max_value=10, value=-2, step=1, help="testing!", on_change=t1)
  44. #选择滑动条
  45. h = st.select_slider(label='select_slider', options=[1,'test2',3], value=3, help="testing!")
  46. #文本框
  47. i = st.text_input(label='text_input', max_chars=30, value='test1', help='testing!', placeholder='请输入')
  48. #数字选择框
  49. j = st.number_input("number_input", min_value=-10, max_value=10, value=2, step=2, help="testing")
  50. #文本区域
  51. k = st.text_area("text_area", value="test1",max_chars=60, help="testing!", placeholder="请输入")
  52. #时间选择
  53. dt1 = datetime.today()
  54. dt2 = datetime.today()
  55. l = st.date_input(label="date_input", value=(dt1,dt2))
  56. #时间选择
  57. m = st.time_input("time_input", value=None, help="testing!")
  58. #上传按钮
  59. n = st.file_uploader(label='file_uploader', accept_multiple_files=True, help="testing!")
  60. #拾色器
  61. o = st.color_picker('color_picker', '#00f900')
  62. #图片
  63. p = st.image(image=['https://i.bmp.ovh/imgs/2021/10/3fd6c4674301c708.jpg',"./data/testimage.jpg"])
  64. #音频
  65. q = st.audio("http://music.163.com/song/media/outer/url?id=1901371647.mp3")
  66. #视频
  67. #html外链
  68. #arg = "<iframe src=\"//player.bilibili.com/player.html?aid=376524564&bvid=BV1wo4y1X7Tk&cid=365010431&page=1\" scrolling=\"yes\" border=\"100\" frameborder=\"yes\" framespacing=\"0\" allowfullscreen=\"true\"> </iframe>"
  69. #r = st.markdown(unsafe_allow_html=True, body=arg)
  70. #iframe conponent
  71. components.iframe(src="//player.bilibili.com/player.html?aid=376524564&bvid=BV1wo4y1X7Tk&cid=365010431&page=1", width=1080, height=720, scrolling=False)
  72. #video
  73. r = st.video("./data/testybb.mp4")
  74. #边栏
  75. add_selectbox = st.sidebar.selectbox(
  76. label="How would you like to be contacted?",
  77. options=("Email", "Home phone", "Mobile phone"),
  78. key="t1"
  79. )
  80. add_selectbox2 = st.sidebar.selectbox(
  81. label="How would you like to be contacted?",
  82. options=("Email", "Home phone", "Mobile phone"),
  83. key="t2"
  84. )
  85. #列布局
  86. col1, col2, col3 = st.columns(3)
  87. with col1:
  88. st.header("A cat")
  89. st.image("https://static.streamlit.io/examples/cat.jpg")
  90. with col2:
  91. st.header("A dog")
  92. st.image("https://static.streamlit.io/examples/dog.jpg")
  93. with col3:
  94. st.header("An owl")
  95. st.image("https://static.streamlit.io/examples/owl.jpg")
  96. #展开框
  97. with st.expander(label="expander", expanded=False):
  98. st.write("tesing")
  99. #container
  100. with st.container():
  101. st.write("container")
  102. container = st.container()
  103. container.write("containertext1")
  104. st.write("not container")
  105. #在container中继续调用组件
  106. container.write("containertext2")
  107. #empty
  108. #with st.empty():
  109. # st.write("not empty here")
  110. #
  111. #empty = st.empty()
  112. #empty.text("still not empty")
  113. #time.sleep(3) #替换
  114. #empty.text("change")
  115. #time.sleep(1) #清除
  116. #empty.empty()
  117. #progress
  118. #my_bar = st.progress(100)
  119. #for percent_complete in range(100).__reversed__():
  120. # time.sleep(0.01)
  121. # my_bar.progress(percent_complete)
  122. #spinner
  123. #with st.spinner('Wait for it...'):
  124. # time.sleep(3)
  125. #st.success('Done!')
  126. #放气球
  127. #st.balloons()
  128. #错误信息
  129. st.error('error!💀')
  130. #警告信息
  131. st.warning("warning! :warning:")
  132. #信息
  133. st.info('message ℹ')
  134. #成功
  135. st.success("success 🎉")
  136. #exception
  137. e = RuntimeError("an exception")
  138. st.exception(e)
  139. #stop
  140. name = st.text_input('Name')
  141. if not name:
  142. st.warning('Please input a name.')
  143. st.stop()
  144. st.success('Thank you for inputting a name.')
  145. #form表单
  146. #with st.form(key="my_form1"):
  147. # st.write("Inside the form")
  148. # slider_val = st.slider("Form slider")
  149. # checkbox_val = st.checkbox("Form checkbox")
  150. #提交
  151. # submitted = st.form_submit_button("Submit")
  152. # if submitted:
  153. # st.write("slider", slider_val, "checkbox", checkbox_val)
  154. #st.write("Outside the form")
  155. form = st.form(key="my_form2")
  156. form.slider("Inside the form")
  157. form.form_submit_button("Submit")
  158. #echo
  159. with st.echo("below"):
  160. st.write('This code will be printed')
  161. #help
  162. st.help(st.help)
  163. #add_rows
  164. df1 = pd.DataFrame(
  165. np.random.randn(1, 5),
  166. columns=('col %d' % i for i in range(5)))
  167. my_table = st.table(df1)
  168. df2 = pd.DataFrame(
  169. np.random.randn(2, 5),
  170. columns=('col %d' % i for i in range(5)))
  171. my_table.add_rows(df2)
  172. #emoji
  173. st.markdown(":smile:😁")
  174. st.text("😁")
  175. st.text(type(r))