streamlit-data-editor-plus 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (456) hide show
  1. streamlit/__init__.py +292 -0
  2. streamlit/__main__.py +20 -0
  3. streamlit/auth_util.py +583 -0
  4. streamlit/cli_util.py +107 -0
  5. streamlit/column_config.py +60 -0
  6. streamlit/commands/__init__.py +13 -0
  7. streamlit/commands/echo.py +128 -0
  8. streamlit/commands/execution_control.py +318 -0
  9. streamlit/commands/logo.py +250 -0
  10. streamlit/commands/navigation.py +430 -0
  11. streamlit/commands/page_config.py +335 -0
  12. streamlit/components/__init__.py +13 -0
  13. streamlit/components/lib/__init__.py +13 -0
  14. streamlit/components/lib/local_component_registry.py +84 -0
  15. streamlit/components/types/__init__.py +13 -0
  16. streamlit/components/types/base_component_registry.py +99 -0
  17. streamlit/components/types/base_custom_component.py +158 -0
  18. streamlit/components/v1/__init__.py +29 -0
  19. streamlit/components/v1/component_arrow.py +141 -0
  20. streamlit/components/v1/component_registry.py +147 -0
  21. streamlit/components/v1/components.py +38 -0
  22. streamlit/components/v1/custom_component.py +239 -0
  23. streamlit/components/v2/__init__.py +531 -0
  24. streamlit/components/v2/bidi_component/__init__.py +20 -0
  25. streamlit/components/v2/bidi_component/constants.py +29 -0
  26. streamlit/components/v2/bidi_component/main.py +537 -0
  27. streamlit/components/v2/bidi_component/serialization.py +272 -0
  28. streamlit/components/v2/bidi_component/state.py +92 -0
  29. streamlit/components/v2/component_definition_resolver.py +143 -0
  30. streamlit/components/v2/component_file_watcher.py +403 -0
  31. streamlit/components/v2/component_manager.py +439 -0
  32. streamlit/components/v2/component_manifest_handler.py +122 -0
  33. streamlit/components/v2/component_path_utils.py +233 -0
  34. streamlit/components/v2/component_registry.py +426 -0
  35. streamlit/components/v2/get_bidi_component_manager.py +51 -0
  36. streamlit/components/v2/manifest_scanner.py +620 -0
  37. streamlit/components/v2/presentation.py +198 -0
  38. streamlit/components/v2/types.py +317 -0
  39. streamlit/config.py +2977 -0
  40. streamlit/config_option.py +319 -0
  41. streamlit/config_util.py +887 -0
  42. streamlit/connections/__init__.py +32 -0
  43. streamlit/connections/base_connection.py +215 -0
  44. streamlit/connections/snowflake_connection.py +765 -0
  45. streamlit/connections/snowpark_connection.py +213 -0
  46. streamlit/connections/sql_connection.py +427 -0
  47. streamlit/connections/util.py +97 -0
  48. streamlit/cursor.py +314 -0
  49. streamlit/dataframe_util.py +1434 -0
  50. streamlit/delta_generator.py +696 -0
  51. streamlit/delta_generator_singletons.py +243 -0
  52. streamlit/deprecation_util.py +253 -0
  53. streamlit/development.py +21 -0
  54. streamlit/elements/__init__.py +13 -0
  55. streamlit/elements/alert.py +341 -0
  56. streamlit/elements/arrow.py +1065 -0
  57. streamlit/elements/balloons.py +47 -0
  58. streamlit/elements/bokeh_chart.py +75 -0
  59. streamlit/elements/code.py +154 -0
  60. streamlit/elements/deck_gl_json_chart.py +627 -0
  61. streamlit/elements/dialog_decorator.py +320 -0
  62. streamlit/elements/empty.py +130 -0
  63. streamlit/elements/exception.py +370 -0
  64. streamlit/elements/form.py +481 -0
  65. streamlit/elements/graphviz_chart.py +226 -0
  66. streamlit/elements/heading.py +407 -0
  67. streamlit/elements/help.py +565 -0
  68. streamlit/elements/html.py +189 -0
  69. streamlit/elements/iframe.py +245 -0
  70. streamlit/elements/image.py +221 -0
  71. streamlit/elements/json.py +171 -0
  72. streamlit/elements/layouts.py +1534 -0
  73. streamlit/elements/lib/__init__.py +13 -0
  74. streamlit/elements/lib/built_in_chart_utils.py +1316 -0
  75. streamlit/elements/lib/color_util.py +272 -0
  76. streamlit/elements/lib/column_config_utils.py +555 -0
  77. streamlit/elements/lib/column_types.py +2699 -0
  78. streamlit/elements/lib/dialog.py +212 -0
  79. streamlit/elements/lib/dicttools.py +152 -0
  80. streamlit/elements/lib/file_uploader_utils.py +91 -0
  81. streamlit/elements/lib/form_utils.py +77 -0
  82. streamlit/elements/lib/image_utils.py +447 -0
  83. streamlit/elements/lib/js_number.py +105 -0
  84. streamlit/elements/lib/layout_utils.py +325 -0
  85. streamlit/elements/lib/mutable_expander_container.py +73 -0
  86. streamlit/elements/lib/mutable_popover_container.py +73 -0
  87. streamlit/elements/lib/mutable_status_container.py +194 -0
  88. streamlit/elements/lib/mutable_tab_container.py +73 -0
  89. streamlit/elements/lib/options_selector_utils.py +480 -0
  90. streamlit/elements/lib/pandas_styler_utils.py +302 -0
  91. streamlit/elements/lib/policies.py +198 -0
  92. streamlit/elements/lib/shortcut_utils.py +152 -0
  93. streamlit/elements/lib/streamlit_plotly_theme.py +206 -0
  94. streamlit/elements/lib/subtitle_utils.py +175 -0
  95. streamlit/elements/lib/utils.py +274 -0
  96. streamlit/elements/map.py +526 -0
  97. streamlit/elements/markdown.py +529 -0
  98. streamlit/elements/media.py +843 -0
  99. streamlit/elements/metric.py +529 -0
  100. streamlit/elements/pdf.py +190 -0
  101. streamlit/elements/plotly_chart.py +779 -0
  102. streamlit/elements/progress.py +172 -0
  103. streamlit/elements/pyplot.py +240 -0
  104. streamlit/elements/snow.py +47 -0
  105. streamlit/elements/space.py +121 -0
  106. streamlit/elements/spinner.py +152 -0
  107. streamlit/elements/table.py +240 -0
  108. streamlit/elements/text.py +113 -0
  109. streamlit/elements/toast.py +175 -0
  110. streamlit/elements/vega_charts.py +2510 -0
  111. streamlit/elements/widgets/__init__.py +13 -0
  112. streamlit/elements/widgets/audio_input.py +334 -0
  113. streamlit/elements/widgets/button.py +1559 -0
  114. streamlit/elements/widgets/button_group.py +1061 -0
  115. streamlit/elements/widgets/camera_input.py +281 -0
  116. streamlit/elements/widgets/chat.py +1028 -0
  117. streamlit/elements/widgets/checkbox.py +420 -0
  118. streamlit/elements/widgets/color_picker.py +329 -0
  119. streamlit/elements/widgets/data_editor.py +1168 -0
  120. streamlit/elements/widgets/data_editor_plus.py +902 -0
  121. streamlit/elements/widgets/feedback.py +322 -0
  122. streamlit/elements/widgets/file_uploader.py +592 -0
  123. streamlit/elements/widgets/multiselect.py +654 -0
  124. streamlit/elements/widgets/number_input.py +719 -0
  125. streamlit/elements/widgets/radio.py +551 -0
  126. streamlit/elements/widgets/select_slider.py +568 -0
  127. streamlit/elements/widgets/selectbox.py +680 -0
  128. streamlit/elements/widgets/slider.py +1093 -0
  129. streamlit/elements/widgets/text_widgets.py +779 -0
  130. streamlit/elements/widgets/time_widgets.py +1712 -0
  131. streamlit/elements/write.py +585 -0
  132. streamlit/emojis.py +34 -0
  133. streamlit/env_util.py +56 -0
  134. streamlit/error_util.py +112 -0
  135. streamlit/errors.py +680 -0
  136. streamlit/external/__init__.py +13 -0
  137. streamlit/external/langchain/__init__.py +23 -0
  138. streamlit/external/langchain/streamlit_callback_handler.py +410 -0
  139. streamlit/file_util.py +266 -0
  140. streamlit/git_util.py +200 -0
  141. streamlit/hello/__init__.py +13 -0
  142. streamlit/hello/__pycache__/__init__.cpython-312.pyc +0 -0
  143. streamlit/hello/__pycache__/streamlit_app.cpython-312.pyc +0 -0
  144. streamlit/hello/animation_demo.py +82 -0
  145. streamlit/hello/dataframe_demo.py +71 -0
  146. streamlit/hello/hello.py +46 -0
  147. streamlit/hello/mapping_demo.py +113 -0
  148. streamlit/hello/plotting_demo.py +62 -0
  149. streamlit/hello/streamlit_app.py +57 -0
  150. streamlit/hello/utils.py +30 -0
  151. streamlit/logger.py +129 -0
  152. streamlit/material_icon_names.py +25 -0
  153. streamlit/navigation/__init__.py +13 -0
  154. streamlit/navigation/page.py +365 -0
  155. streamlit/net_util.py +125 -0
  156. streamlit/path_security.py +98 -0
  157. streamlit/platform.py +31 -0
  158. streamlit/proto/Alert_pb2.py +28 -0
  159. streamlit/proto/Alert_pb2.pyi +100 -0
  160. streamlit/proto/AppPage_pb2.py +25 -0
  161. streamlit/proto/AppPage_pb2.pyi +75 -0
  162. streamlit/proto/ArrowData_pb2.py +27 -0
  163. streamlit/proto/ArrowData_pb2.pyi +103 -0
  164. streamlit/proto/ArrowNamedDataSet_pb2.py +26 -0
  165. streamlit/proto/ArrowNamedDataSet_pb2.pyi +65 -0
  166. streamlit/proto/AudioInput_pb2.py +26 -0
  167. streamlit/proto/AudioInput_pb2.pyi +72 -0
  168. streamlit/proto/Audio_pb2.py +26 -0
  169. streamlit/proto/Audio_pb2.pyi +75 -0
  170. streamlit/proto/AuthRedirect_pb2.py +25 -0
  171. streamlit/proto/AuthRedirect_pb2.pyi +48 -0
  172. streamlit/proto/AutoRerun_pb2.py +25 -0
  173. streamlit/proto/AutoRerun_pb2.pyi +52 -0
  174. streamlit/proto/BackMsg_pb2.py +29 -0
  175. streamlit/proto/BackMsg_pb2.pyi +132 -0
  176. streamlit/proto/Balloons_pb2.py +25 -0
  177. streamlit/proto/Balloons_pb2.pyi +48 -0
  178. streamlit/proto/BidiComponent_pb2.py +32 -0
  179. streamlit/proto/BidiComponent_pb2.pyi +176 -0
  180. streamlit/proto/Block_pb2.py +62 -0
  181. streamlit/proto/Block_pb2.pyi +518 -0
  182. streamlit/proto/ButtonGroup_pb2.py +32 -0
  183. streamlit/proto/ButtonGroup_pb2.pyi +157 -0
  184. streamlit/proto/ButtonLikeIconPosition_pb2.py +25 -0
  185. streamlit/proto/ButtonLikeIconPosition_pb2.pyi +47 -0
  186. streamlit/proto/Button_pb2.py +26 -0
  187. streamlit/proto/Button_pb2.pyi +82 -0
  188. streamlit/proto/CameraInput_pb2.py +26 -0
  189. streamlit/proto/CameraInput_pb2.pyi +66 -0
  190. streamlit/proto/ChatInput_pb2.py +27 -0
  191. streamlit/proto/ChatInput_pb2.pyi +113 -0
  192. streamlit/proto/Checkbox_pb2.py +28 -0
  193. streamlit/proto/Checkbox_pb2.pyi +99 -0
  194. streamlit/proto/ClientState_pb2.py +28 -0
  195. streamlit/proto/ClientState_pb2.pyi +137 -0
  196. streamlit/proto/Code_pb2.py +25 -0
  197. streamlit/proto/Code_pb2.pyi +59 -0
  198. streamlit/proto/ColorPicker_pb2.py +26 -0
  199. streamlit/proto/ColorPicker_pb2.pyi +82 -0
  200. streamlit/proto/Common_pb2.py +49 -0
  201. streamlit/proto/Common_pb2.pyi +325 -0
  202. streamlit/proto/Components_pb2.py +33 -0
  203. streamlit/proto/Components_pb2.pyi +196 -0
  204. streamlit/proto/Dataframe_pb2.py +30 -0
  205. streamlit/proto/Dataframe_pb2.pyi +172 -0
  206. streamlit/proto/DateInput_pb2.py +26 -0
  207. streamlit/proto/DateInput_pb2.pyi +91 -0
  208. streamlit/proto/DateTimeInput_pb2.py +26 -0
  209. streamlit/proto/DateTimeInput_pb2.pyi +94 -0
  210. streamlit/proto/DeckGlJsonChart_pb2.py +27 -0
  211. streamlit/proto/DeckGlJsonChart_pb2.pyi +91 -0
  212. streamlit/proto/Delta_pb2.py +29 -0
  213. streamlit/proto/Delta_pb2.pyi +82 -0
  214. streamlit/proto/DownloadButton_pb2.py +26 -0
  215. streamlit/proto/DownloadButton_pb2.pyi +92 -0
  216. streamlit/proto/Element_pb2.py +81 -0
  217. streamlit/proto/Element_pb2.pyi +348 -0
  218. streamlit/proto/Empty_pb2.py +25 -0
  219. streamlit/proto/Empty_pb2.pyi +42 -0
  220. streamlit/proto/Exception_pb2.py +26 -0
  221. streamlit/proto/Exception_pb2.pyi +88 -0
  222. streamlit/proto/Favicon_pb2.py +25 -0
  223. streamlit/proto/Favicon_pb2.pyi +47 -0
  224. streamlit/proto/Feedback_pb2.py +27 -0
  225. streamlit/proto/Feedback_pb2.pyi +93 -0
  226. streamlit/proto/FileUploader_pb2.py +26 -0
  227. streamlit/proto/FileUploader_pb2.pyi +90 -0
  228. streamlit/proto/ForwardMsg_pb2.py +48 -0
  229. streamlit/proto/ForwardMsg_pb2.pyi +296 -0
  230. streamlit/proto/GapSize_pb2.py +27 -0
  231. streamlit/proto/GapSize_pb2.pyi +82 -0
  232. streamlit/proto/GitInfo_pb2.py +27 -0
  233. streamlit/proto/GitInfo_pb2.pyi +84 -0
  234. streamlit/proto/GraphVizChart_pb2.py +25 -0
  235. streamlit/proto/GraphVizChart_pb2.pyi +56 -0
  236. streamlit/proto/Heading_pb2.py +25 -0
  237. streamlit/proto/Heading_pb2.pyi +63 -0
  238. streamlit/proto/HeightConfig_pb2.py +25 -0
  239. streamlit/proto/HeightConfig_pb2.pyi +61 -0
  240. streamlit/proto/Help_pb2.py +27 -0
  241. streamlit/proto/Help_pb2.pyi +104 -0
  242. streamlit/proto/Html_pb2.py +25 -0
  243. streamlit/proto/Html_pb2.pyi +52 -0
  244. streamlit/proto/IFrame_pb2.py +25 -0
  245. streamlit/proto/IFrame_pb2.pyi +68 -0
  246. streamlit/proto/Image_pb2.py +27 -0
  247. streamlit/proto/Image_pb2.pyi +73 -0
  248. streamlit/proto/Json_pb2.py +25 -0
  249. streamlit/proto/Json_pb2.pyi +63 -0
  250. streamlit/proto/LabelVisibility_pb2.py +27 -0
  251. streamlit/proto/LabelVisibility_pb2.pyi +69 -0
  252. streamlit/proto/LinkButton_pb2.py +26 -0
  253. streamlit/proto/LinkButton_pb2.pyi +76 -0
  254. streamlit/proto/Logo_pb2.py +27 -0
  255. streamlit/proto/Logo_pb2.pyi +82 -0
  256. streamlit/proto/Markdown_pb2.py +27 -0
  257. streamlit/proto/Markdown_pb2.pyi +83 -0
  258. streamlit/proto/Metric_pb2.py +32 -0
  259. streamlit/proto/Metric_pb2.pyi +145 -0
  260. streamlit/proto/MetricsEvent_pb2.py +28 -0
  261. streamlit/proto/MetricsEvent_pb2.pyi +224 -0
  262. streamlit/proto/MultiSelect_pb2.py +26 -0
  263. streamlit/proto/MultiSelect_pb2.pyi +108 -0
  264. streamlit/proto/Navigation_pb2.py +28 -0
  265. streamlit/proto/Navigation_pb2.pyi +89 -0
  266. streamlit/proto/NewSession_pb2.py +47 -0
  267. streamlit/proto/NewSession_pb2.pyi +753 -0
  268. streamlit/proto/NumberInput_pb2.py +28 -0
  269. streamlit/proto/NumberInput_pb2.pyi +138 -0
  270. streamlit/proto/PageConfig_pb2.py +33 -0
  271. streamlit/proto/PageConfig_pb2.pyi +179 -0
  272. streamlit/proto/PageInfo_pb2.py +25 -0
  273. streamlit/proto/PageInfo_pb2.pyi +50 -0
  274. streamlit/proto/PageLink_pb2.py +26 -0
  275. streamlit/proto/PageLink_pb2.pyi +80 -0
  276. streamlit/proto/PageNotFound_pb2.py +25 -0
  277. streamlit/proto/PageNotFound_pb2.pyi +49 -0
  278. streamlit/proto/PageProfile_pb2.py +29 -0
  279. streamlit/proto/PageProfile_pb2.pyi +146 -0
  280. streamlit/proto/ParentMessage_pb2.py +25 -0
  281. streamlit/proto/ParentMessage_pb2.pyi +53 -0
  282. streamlit/proto/PlotlyChart_pb2.py +27 -0
  283. streamlit/proto/PlotlyChart_pb2.pyi +96 -0
  284. streamlit/proto/Progress_pb2.py +25 -0
  285. streamlit/proto/Progress_pb2.pyi +50 -0
  286. streamlit/proto/Radio_pb2.py +26 -0
  287. streamlit/proto/Radio_pb2.pyi +104 -0
  288. streamlit/proto/RootContainer_pb2.py +25 -0
  289. streamlit/proto/RootContainer_pb2.pyi +56 -0
  290. streamlit/proto/Selectbox_pb2.py +26 -0
  291. streamlit/proto/Selectbox_pb2.pyi +107 -0
  292. streamlit/proto/SessionEvent_pb2.py +26 -0
  293. streamlit/proto/SessionEvent_pb2.pyi +72 -0
  294. streamlit/proto/SessionStatus_pb2.py +25 -0
  295. streamlit/proto/SessionStatus_pb2.pyi +64 -0
  296. streamlit/proto/Skeleton_pb2.py +27 -0
  297. streamlit/proto/Skeleton_pb2.pyi +75 -0
  298. streamlit/proto/Slider_pb2.py +30 -0
  299. streamlit/proto/Slider_pb2.pyi +161 -0
  300. streamlit/proto/Snow_pb2.py +25 -0
  301. streamlit/proto/Snow_pb2.pyi +48 -0
  302. streamlit/proto/Space_pb2.py +25 -0
  303. streamlit/proto/Space_pb2.pyi +48 -0
  304. streamlit/proto/Spinner_pb2.py +25 -0
  305. streamlit/proto/Spinner_pb2.pyi +56 -0
  306. streamlit/proto/Table_pb2.py +28 -0
  307. streamlit/proto/Table_pb2.pyi +83 -0
  308. streamlit/proto/TextAlignmentConfig_pb2.py +27 -0
  309. streamlit/proto/TextAlignmentConfig_pb2.pyi +69 -0
  310. streamlit/proto/TextArea_pb2.py +26 -0
  311. streamlit/proto/TextArea_pb2.pyi +97 -0
  312. streamlit/proto/TextInput_pb2.py +28 -0
  313. streamlit/proto/TextInput_pb2.pyi +124 -0
  314. streamlit/proto/Text_pb2.py +25 -0
  315. streamlit/proto/Text_pb2.pyi +53 -0
  316. streamlit/proto/TimeInput_pb2.py +26 -0
  317. streamlit/proto/TimeInput_pb2.pyi +86 -0
  318. streamlit/proto/Toast_pb2.py +25 -0
  319. streamlit/proto/Toast_pb2.pyi +64 -0
  320. streamlit/proto/Transient_pb2.py +26 -0
  321. streamlit/proto/Transient_pb2.pyi +53 -0
  322. streamlit/proto/VegaLiteChart_pb2.py +27 -0
  323. streamlit/proto/VegaLiteChart_pb2.pyi +92 -0
  324. streamlit/proto/Video_pb2.py +30 -0
  325. streamlit/proto/Video_pb2.pyi +129 -0
  326. streamlit/proto/WidgetStates_pb2.py +31 -0
  327. streamlit/proto/WidgetStates_pb2.pyi +157 -0
  328. streamlit/proto/WidthConfig_pb2.py +25 -0
  329. streamlit/proto/WidthConfig_pb2.pyi +61 -0
  330. streamlit/proto/__init__.py +15 -0
  331. streamlit/proto/openmetrics_data_model_pb2.py +58 -0
  332. streamlit/proto/openmetrics_data_model_pb2.pyi +558 -0
  333. streamlit/py.typed +0 -0
  334. streamlit/runtime/__init__.py +50 -0
  335. streamlit/runtime/app_session.py +1302 -0
  336. streamlit/runtime/caching/__init__.py +118 -0
  337. streamlit/runtime/caching/cache_data_api.py +775 -0
  338. streamlit/runtime/caching/cache_errors.py +143 -0
  339. streamlit/runtime/caching/cache_resource_api.py +756 -0
  340. streamlit/runtime/caching/cache_type.py +33 -0
  341. streamlit/runtime/caching/cache_utils.py +601 -0
  342. streamlit/runtime/caching/cached_message_replay.py +290 -0
  343. streamlit/runtime/caching/hashing.py +655 -0
  344. streamlit/runtime/caching/legacy_cache_api.py +170 -0
  345. streamlit/runtime/caching/storage/__init__.py +29 -0
  346. streamlit/runtime/caching/storage/cache_storage_protocol.py +236 -0
  347. streamlit/runtime/caching/storage/dummy_cache_storage.py +60 -0
  348. streamlit/runtime/caching/storage/in_memory_cache_storage_wrapper.py +159 -0
  349. streamlit/runtime/caching/storage/local_disk_cache_storage.py +223 -0
  350. streamlit/runtime/caching/ttl_cleanup_cache.py +85 -0
  351. streamlit/runtime/connection_factory.py +498 -0
  352. streamlit/runtime/context.py +449 -0
  353. streamlit/runtime/context_util.py +49 -0
  354. streamlit/runtime/credentials.py +355 -0
  355. streamlit/runtime/download_data_util.py +53 -0
  356. streamlit/runtime/forward_msg_cache.py +101 -0
  357. streamlit/runtime/forward_msg_queue.py +263 -0
  358. streamlit/runtime/fragment.py +441 -0
  359. streamlit/runtime/media_file_manager.py +409 -0
  360. streamlit/runtime/media_file_storage.py +143 -0
  361. streamlit/runtime/memory_media_file_storage.py +201 -0
  362. streamlit/runtime/memory_session_storage.py +77 -0
  363. streamlit/runtime/memory_uploaded_file_manager.py +149 -0
  364. streamlit/runtime/metrics_util.py +612 -0
  365. streamlit/runtime/pages_manager.py +160 -0
  366. streamlit/runtime/runtime.py +775 -0
  367. streamlit/runtime/runtime_util.py +108 -0
  368. streamlit/runtime/script_data.py +46 -0
  369. streamlit/runtime/scriptrunner/__init__.py +38 -0
  370. streamlit/runtime/scriptrunner/exec_code.py +167 -0
  371. streamlit/runtime/scriptrunner/magic.py +277 -0
  372. streamlit/runtime/scriptrunner/magic_funcs.py +32 -0
  373. streamlit/runtime/scriptrunner/script_cache.py +89 -0
  374. streamlit/runtime/scriptrunner/script_runner.py +805 -0
  375. streamlit/runtime/scriptrunner_utils/__init__.py +19 -0
  376. streamlit/runtime/scriptrunner_utils/exceptions.py +44 -0
  377. streamlit/runtime/scriptrunner_utils/script_requests.py +311 -0
  378. streamlit/runtime/scriptrunner_utils/script_run_context.py +277 -0
  379. streamlit/runtime/secrets.py +533 -0
  380. streamlit/runtime/session_manager.py +430 -0
  381. streamlit/runtime/state/__init__.py +47 -0
  382. streamlit/runtime/state/common.py +256 -0
  383. streamlit/runtime/state/presentation.py +85 -0
  384. streamlit/runtime/state/query_params.py +767 -0
  385. streamlit/runtime/state/query_params_proxy.py +222 -0
  386. streamlit/runtime/state/safe_session_state.py +146 -0
  387. streamlit/runtime/state/session_state.py +1299 -0
  388. streamlit/runtime/state/session_state_proxy.py +153 -0
  389. streamlit/runtime/state/widgets.py +197 -0
  390. streamlit/runtime/stats.py +356 -0
  391. streamlit/runtime/theme_util.py +148 -0
  392. streamlit/runtime/uploaded_file_manager.py +152 -0
  393. streamlit/runtime/websocket_session_manager.py +301 -0
  394. streamlit/source_util.py +97 -0
  395. streamlit/starlette.py +34 -0
  396. streamlit/string_util.py +264 -0
  397. streamlit/temporary_directory.py +67 -0
  398. streamlit/testing/__init__.py +13 -0
  399. streamlit/testing/v1/__init__.py +17 -0
  400. streamlit/testing/v1/app_test.py +1089 -0
  401. streamlit/testing/v1/element_tree.py +2272 -0
  402. streamlit/testing/v1/local_script_runner.py +176 -0
  403. streamlit/testing/v1/util.py +61 -0
  404. streamlit/time_util.py +73 -0
  405. streamlit/type_util.py +480 -0
  406. streamlit/url_util.py +120 -0
  407. streamlit/user_info.py +698 -0
  408. streamlit/util.py +108 -0
  409. streamlit/vendor/__init__.py +0 -0
  410. streamlit/vendor/pympler/__init__.py +0 -0
  411. streamlit/vendor/pympler/asizeof.py +2870 -0
  412. streamlit/version.py +18 -0
  413. streamlit/watcher/__init__.py +28 -0
  414. streamlit/watcher/event_based_path_watcher.py +500 -0
  415. streamlit/watcher/folder_black_list.py +82 -0
  416. streamlit/watcher/local_sources_watcher.py +297 -0
  417. streamlit/watcher/path_watcher.py +244 -0
  418. streamlit/watcher/polling_path_watcher.py +138 -0
  419. streamlit/watcher/util.py +223 -0
  420. streamlit/web/__init__.py +13 -0
  421. streamlit/web/bootstrap.py +463 -0
  422. streamlit/web/cache_storage_manager_config.py +38 -0
  423. streamlit/web/cli.py +465 -0
  424. streamlit/web/server/__init__.py +30 -0
  425. streamlit/web/server/app_discovery.py +422 -0
  426. streamlit/web/server/app_static_file_handler.py +102 -0
  427. streamlit/web/server/authlib_tornado_integration.py +125 -0
  428. streamlit/web/server/bidi_component_request_handler.py +193 -0
  429. streamlit/web/server/browser_websocket_handler.py +341 -0
  430. streamlit/web/server/component_file_utils.py +105 -0
  431. streamlit/web/server/component_request_handler.py +107 -0
  432. streamlit/web/server/media_file_handler.py +148 -0
  433. streamlit/web/server/oauth_authlib_routes.py +314 -0
  434. streamlit/web/server/oidc_mixin.py +138 -0
  435. streamlit/web/server/routes.py +257 -0
  436. streamlit/web/server/server.py +555 -0
  437. streamlit/web/server/server_util.py +235 -0
  438. streamlit/web/server/starlette/__init__.py +23 -0
  439. streamlit/web/server/starlette/starlette_app.py +541 -0
  440. streamlit/web/server/starlette/starlette_app_utils.py +306 -0
  441. streamlit/web/server/starlette/starlette_auth_routes.py +584 -0
  442. streamlit/web/server/starlette/starlette_gzip_middleware.py +121 -0
  443. streamlit/web/server/starlette/starlette_path_security_middleware.py +97 -0
  444. streamlit/web/server/starlette/starlette_routes.py +884 -0
  445. streamlit/web/server/starlette/starlette_server.py +496 -0
  446. streamlit/web/server/starlette/starlette_server_config.py +55 -0
  447. streamlit/web/server/starlette/starlette_static_routes.py +181 -0
  448. streamlit/web/server/starlette/starlette_websocket.py +560 -0
  449. streamlit/web/server/stats_request_handler.py +116 -0
  450. streamlit/web/server/upload_file_request_handler.py +158 -0
  451. streamlit/web/server/websocket_headers.py +56 -0
  452. streamlit_data_editor_plus-0.1.0.dist-info/METADATA +76 -0
  453. streamlit_data_editor_plus-0.1.0.dist-info/RECORD +456 -0
  454. streamlit_data_editor_plus-0.1.0.dist-info/WHEEL +5 -0
  455. streamlit_data_editor_plus-0.1.0.dist-info/entry_points.txt +2 -0
  456. streamlit_data_editor_plus-0.1.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,23 @@
1
+ # Copyright (c) Streamlit Inc. (2018-2022) Snowflake Inc. (2022-2026)
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ from streamlit.external.langchain.streamlit_callback_handler import (
16
+ LLMThoughtLabeler,
17
+ StreamlitCallbackHandler,
18
+ )
19
+
20
+ __all__ = [
21
+ "LLMThoughtLabeler",
22
+ "StreamlitCallbackHandler",
23
+ ]
@@ -0,0 +1,410 @@
1
+ # Copyright (c) Streamlit Inc. (2018-2022) Snowflake Inc. (2022-2026)
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ """
16
+ LangChain CallbackHandler that prints to streamlit.
17
+
18
+ This is a special API that's imported and used by LangChain itself. Any updates
19
+ to the public API (the StreamlitCallbackHandler constructor, and the entirety
20
+ of LLMThoughtLabeler) *must* remain backwards-compatible to avoid breaking
21
+ LangChain.
22
+
23
+ This means that it's acceptable to add new optional kwargs to StreamlitCallbackHandler,
24
+ but no new positional args or required kwargs should be added, and no existing
25
+ args should be removed. If we need to overhaul the API, we must ensure that a
26
+ compatible API continues to exist.
27
+
28
+ Any major change to the StreamlitCallbackHandler should be tested by importing
29
+ the API *from LangChain itself*.
30
+
31
+ This module is lazy-loaded.
32
+ """
33
+
34
+ # NOTE: We ignore all mypy import-not-found errors as top-level since
35
+ # this module is optional and the langchain dependency is not installed
36
+ # by default.
37
+ # mypy: disable-error-code="import-not-found, unused-ignore, misc"
38
+
39
+ # Deactivate unused argument errors for this file since we need lots of
40
+ # unused arguments to comply with the LangChain callback interface.
41
+ # ruff: noqa: ARG002
42
+
43
+ from __future__ import annotations
44
+
45
+ import time
46
+ from enum import Enum
47
+ from typing import TYPE_CHECKING, Any, NamedTuple
48
+
49
+ from langchain.callbacks.base import (
50
+ BaseCallbackHandler,
51
+ )
52
+
53
+ from streamlit.runtime.metrics_util import gather_metrics
54
+
55
+ if TYPE_CHECKING:
56
+ from langchain.schema import (
57
+ AgentAction,
58
+ AgentFinish,
59
+ LLMResult,
60
+ )
61
+
62
+ from streamlit.delta_generator import DeltaGenerator
63
+ from streamlit.elements.lib.mutable_status_container import StatusContainer
64
+
65
+
66
+ def _convert_newlines(text: str) -> str:
67
+ """Convert newline characters to markdown newline sequences
68
+ (space, space, newline).
69
+ """
70
+ return text.replace("\n", " \n")
71
+
72
+
73
+ # The maximum length of the "input_str" portion of a tool label.
74
+ # Strings that are longer than this will be truncated with "..."
75
+ MAX_TOOL_INPUT_STR_LENGTH = 60
76
+
77
+
78
+ class LLMThoughtState(Enum):
79
+ # The LLM is thinking about what to do next. We don't know which tool we'll run.
80
+ THINKING = "THINKING"
81
+ # The LLM has decided to run a tool. We don't have results from the tool yet.
82
+ RUNNING_TOOL = "RUNNING_TOOL"
83
+ # We have results from the tool.
84
+ COMPLETE = "COMPLETE"
85
+ # The LLM completed with an error.
86
+ ERROR = "ERROR"
87
+
88
+
89
+ class ToolRecord(NamedTuple):
90
+ name: str
91
+ input_str: str
92
+
93
+
94
+ class LLMThoughtLabeler:
95
+ """
96
+ Generates markdown labels for LLMThought containers. Pass a custom
97
+ subclass of this to StreamlitCallbackHandler to override its default
98
+ labeling logic.
99
+ """
100
+
101
+ def get_initial_label(self) -> str:
102
+ """Return the markdown label for a new LLMThought that doesn't have
103
+ an associated tool yet.
104
+ """
105
+ return "Thinking..."
106
+
107
+ def get_tool_label(self, tool: ToolRecord, is_complete: bool) -> str:
108
+ """Return the label for an LLMThought that has an associated
109
+ tool.
110
+
111
+ Parameters
112
+ ----------
113
+ tool
114
+ The tool's ToolRecord
115
+
116
+ is_complete
117
+ True if the thought is complete; False if the thought
118
+ is still receiving input.
119
+
120
+ Returns
121
+ -------
122
+ The markdown label for the thought's container.
123
+
124
+ """
125
+ input_str = tool.input_str
126
+ name = tool.name
127
+ if name == "_Exception":
128
+ name = "Parsing error"
129
+ input_str_len = min(MAX_TOOL_INPUT_STR_LENGTH, len(input_str))
130
+ input_str = input_str[:input_str_len]
131
+ if len(tool.input_str) > input_str_len:
132
+ input_str += "..."
133
+ input_str = input_str.replace("\n", " ")
134
+ return f"**{name}:** {input_str}"
135
+
136
+ def get_final_agent_thought_label(self) -> str:
137
+ """Return the markdown label for the agent's final thought -
138
+ the "Now I have the answer" thought, that doesn't involve
139
+ a tool.
140
+ """
141
+ return "**Complete!**"
142
+
143
+
144
+ class LLMThought:
145
+ """Encapsulates the Streamlit UI for a single LLM 'thought' during a LangChain Agent
146
+ run. Each tool usage gets its own thought; and runs also generally having a
147
+ concluding thought where the Agent determines that it has an answer to the prompt.
148
+
149
+ Each thought gets its own expander UI.
150
+ """
151
+
152
+ def __init__(
153
+ self,
154
+ parent_container: DeltaGenerator,
155
+ labeler: LLMThoughtLabeler,
156
+ expanded: bool,
157
+ collapse_on_complete: bool,
158
+ ) -> None:
159
+ self._container = parent_container.status(
160
+ labeler.get_initial_label(), expanded=expanded
161
+ )
162
+
163
+ self._state = LLMThoughtState.THINKING
164
+ self._llm_token_stream = ""
165
+ self._llm_token_stream_placeholder: DeltaGenerator | None = None
166
+ self._last_tool: ToolRecord | None = None
167
+ self._collapse_on_complete = collapse_on_complete
168
+ self._labeler = labeler
169
+
170
+ @property
171
+ def container(self) -> StatusContainer:
172
+ """The container we're writing into."""
173
+ return self._container
174
+
175
+ @property
176
+ def last_tool(self) -> ToolRecord | None:
177
+ """The last tool executed by this thought."""
178
+ return self._last_tool
179
+
180
+ def _reset_llm_token_stream(self) -> None:
181
+ if self._llm_token_stream_placeholder is not None:
182
+ self._llm_token_stream_placeholder.markdown(self._llm_token_stream)
183
+
184
+ self._llm_token_stream = ""
185
+ self._llm_token_stream_placeholder = None
186
+
187
+ def on_llm_start(self, serialized: dict[str, Any], prompts: list[str]) -> None:
188
+ self._reset_llm_token_stream()
189
+
190
+ def on_llm_new_token(self, token: str, **kwargs: Any) -> None:
191
+ # This is only called when the LLM is initialized with `streaming=True`
192
+ self._llm_token_stream += _convert_newlines(token)
193
+ if self._llm_token_stream_placeholder is None:
194
+ self._llm_token_stream_placeholder = self._container.empty()
195
+ self._llm_token_stream_placeholder.markdown(self._llm_token_stream + "▕")
196
+
197
+ def on_llm_end(self, response: LLMResult, **kwargs: Any) -> None:
198
+ # `response` is the concatenation of all the tokens received by the LLM.
199
+ # If we're receiving streaming tokens from `on_llm_new_token`, this response
200
+ # data is redundant
201
+ self._reset_llm_token_stream()
202
+ # set the container status to complete
203
+ self.complete(self._labeler.get_final_agent_thought_label())
204
+
205
+ def on_llm_error(self, error: BaseException, *args: Any, **kwargs: Any) -> None:
206
+ self._container.exception(error)
207
+ self._state = LLMThoughtState.ERROR
208
+ self.complete("LLM encountered an error...")
209
+
210
+ def on_tool_start(
211
+ self, serialized: dict[str, Any], input_str: str, **kwargs: Any
212
+ ) -> None:
213
+ # Called with the name of the tool we're about to run (in `serialized[name]`),
214
+ # and its input. We change our container's label to be the tool name.
215
+ self._state = LLMThoughtState.RUNNING_TOOL
216
+ tool_name = serialized["name"]
217
+ self._last_tool = ToolRecord(name=tool_name, input_str=input_str)
218
+ self._container.update(
219
+ label=self._labeler.get_tool_label(self._last_tool, is_complete=False),
220
+ state="running",
221
+ )
222
+ if len(input_str) > MAX_TOOL_INPUT_STR_LENGTH:
223
+ # output is printed later in on_tool_end
224
+ self._container.markdown(f"**Input:**\n\n{input_str}\n\n**Output:**")
225
+
226
+ def on_tool_end(
227
+ self,
228
+ output: str,
229
+ color: str | None = None,
230
+ observation_prefix: str | None = None,
231
+ llm_prefix: str | None = None,
232
+ **kwargs: Any,
233
+ ) -> None:
234
+ self._container.markdown(output)
235
+
236
+ def on_tool_error(self, error: BaseException, *args: Any, **kwargs: Any) -> None:
237
+ self._container.markdown("**Tool encountered an error...**")
238
+ self._container.exception(error)
239
+ self._container.update(state="error")
240
+
241
+ def on_agent_action(
242
+ self, action: AgentAction, color: str | None = None, **kwargs: Any
243
+ ) -> Any:
244
+ # Called when we're about to kick off a new tool. The `action` data
245
+ # tells us the tool we're about to use, and the input we'll give it.
246
+ # We don't output anything here, because we'll receive this same data
247
+ # when `on_tool_start` is called immediately after.
248
+ pass
249
+
250
+ def complete(self, final_label: str | None = None) -> None:
251
+ """Finish the thought."""
252
+ if final_label is None and self._state == LLMThoughtState.RUNNING_TOOL:
253
+ if self._last_tool is None:
254
+ raise RuntimeError(
255
+ "_last_tool should never be null when _state == RUNNING_TOOL"
256
+ )
257
+ final_label = self._labeler.get_tool_label(
258
+ self._last_tool, is_complete=True
259
+ )
260
+
261
+ if self._last_tool and self._last_tool.name == "_Exception":
262
+ self._state = LLMThoughtState.ERROR
263
+ elif self._state != LLMThoughtState.ERROR:
264
+ self._state = LLMThoughtState.COMPLETE
265
+
266
+ if self._collapse_on_complete:
267
+ # Add a quick delay to show the user the final output before we collapse
268
+ time.sleep(0.25)
269
+
270
+ self._container.update(
271
+ label=final_label,
272
+ expanded=False if self._collapse_on_complete else None,
273
+ state="error" if self._state == LLMThoughtState.ERROR else "complete",
274
+ )
275
+
276
+
277
+ class StreamlitCallbackHandler(BaseCallbackHandler):
278
+ @gather_metrics("external.langchain.StreamlitCallbackHandler")
279
+ def __init__(
280
+ self,
281
+ parent_container: DeltaGenerator,
282
+ *,
283
+ max_thought_containers: int = 4,
284
+ expand_new_thoughts: bool = False,
285
+ collapse_completed_thoughts: bool = False,
286
+ thought_labeler: LLMThoughtLabeler | None = None,
287
+ ) -> None:
288
+ """Construct a new StreamlitCallbackHandler. This CallbackHandler is geared
289
+ towards use with a LangChain Agent; it displays the Agent's LLM and tool-usage
290
+ "thoughts" inside a series of Streamlit expanders.
291
+
292
+ Parameters
293
+ ----------
294
+ parent_container
295
+ The `st.container` that will contain all the Streamlit elements that the
296
+ Handler creates.
297
+
298
+ max_thought_containers
299
+
300
+ .. note::
301
+ This parameter is deprecated and is ignored in the latest version of
302
+ the callback handler.
303
+
304
+ The max number of completed LLM thought containers to show at once. When
305
+ this threshold is reached, a new thought will cause the oldest thoughts to
306
+ be collapsed into a "History" expander. Defaults to 4.
307
+
308
+ expand_new_thoughts
309
+ Each LLM "thought" gets its own `st.expander`. This param controls whether
310
+ that expander is expanded by default. Defaults to False.
311
+
312
+ collapse_completed_thoughts
313
+ If True, LLM thought expanders will be collapsed when completed.
314
+ Defaults to False.
315
+
316
+ thought_labeler
317
+ An optional custom LLMThoughtLabeler instance. If unspecified, the handler
318
+ will use the default thought labeling logic. Defaults to None.
319
+ """
320
+ self._parent_container = parent_container
321
+ self._history_parent = parent_container.container()
322
+ self._current_thought: LLMThought | None = None
323
+ self._completed_thoughts: list[LLMThought] = []
324
+ self._max_thought_containers = max(max_thought_containers, 1)
325
+ self._expand_new_thoughts = expand_new_thoughts
326
+ self._collapse_completed_thoughts = collapse_completed_thoughts
327
+ self._thought_labeler = thought_labeler or LLMThoughtLabeler()
328
+
329
+ def _require_current_thought(self) -> LLMThought:
330
+ """Return our current LLMThought. Raise an error if we have no current
331
+ thought.
332
+ """
333
+ if self._current_thought is None:
334
+ raise RuntimeError("Current LLMThought is unexpectedly None!")
335
+ return self._current_thought
336
+
337
+ def _get_last_completed_thought(self) -> LLMThought | None:
338
+ """Return our most recent completed LLMThought, or None if we don't have one."""
339
+ if len(self._completed_thoughts) > 0:
340
+ return self._completed_thoughts[len(self._completed_thoughts) - 1]
341
+ return None
342
+
343
+ def _complete_current_thought(self, final_label: str | None = None) -> None:
344
+ """Complete the current thought, optionally assigning it a new label.
345
+ Add it to our _completed_thoughts list.
346
+ """
347
+ thought = self._require_current_thought()
348
+ thought.complete(final_label)
349
+ self._completed_thoughts.append(thought)
350
+ self._current_thought = None
351
+
352
+ def on_llm_start(
353
+ self, serialized: dict[str, Any], prompts: list[str], **kwargs: Any
354
+ ) -> None:
355
+ if self._current_thought is None:
356
+ self._current_thought = LLMThought(
357
+ parent_container=self._parent_container,
358
+ expanded=self._expand_new_thoughts,
359
+ collapse_on_complete=self._collapse_completed_thoughts,
360
+ labeler=self._thought_labeler,
361
+ )
362
+
363
+ self._current_thought.on_llm_start(serialized, prompts)
364
+
365
+ # We don't prune_old_thought_containers here, because our container won't
366
+ # be visible until it has a child.
367
+
368
+ def on_llm_new_token(self, token: str, **kwargs: Any) -> None:
369
+ self._require_current_thought().on_llm_new_token(token, **kwargs)
370
+
371
+ def on_llm_end(self, response: LLMResult, **kwargs: Any) -> None:
372
+ self._require_current_thought().on_llm_end(response, **kwargs)
373
+
374
+ def on_llm_error(self, error: BaseException, *args: Any, **kwargs: Any) -> None:
375
+ self._require_current_thought().on_llm_error(error, **kwargs)
376
+
377
+ def on_tool_start(
378
+ self, serialized: dict[str, Any], input_str: str, **kwargs: Any
379
+ ) -> None:
380
+ self._require_current_thought().on_tool_start(serialized, input_str, **kwargs)
381
+
382
+ def on_tool_end(
383
+ self,
384
+ output: str,
385
+ color: str | None = None,
386
+ observation_prefix: str | None = None,
387
+ llm_prefix: str | None = None,
388
+ **kwargs: Any,
389
+ ) -> None:
390
+ self._require_current_thought().on_tool_end(
391
+ output, color, observation_prefix, llm_prefix, **kwargs
392
+ )
393
+ self._complete_current_thought()
394
+
395
+ def on_tool_error(self, error: BaseException, *args: Any, **kwargs: Any) -> None:
396
+ self._require_current_thought().on_tool_error(error, **kwargs)
397
+
398
+ def on_agent_action(
399
+ self, action: AgentAction, color: str | None = None, **kwargs: Any
400
+ ) -> Any:
401
+ self._require_current_thought().on_agent_action(action, color, **kwargs)
402
+
403
+ def on_agent_finish(
404
+ self, finish: AgentFinish, color: str | None = None, **kwargs: Any
405
+ ) -> None:
406
+ if self._current_thought is not None:
407
+ self._current_thought.complete(
408
+ self._thought_labeler.get_final_agent_thought_label()
409
+ )
410
+ self._current_thought = None
streamlit/file_util.py ADDED
@@ -0,0 +1,266 @@
1
+ # Copyright (c) Streamlit Inc. (2018-2022) Snowflake Inc. (2022-2026)
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ from __future__ import annotations
16
+
17
+ import contextlib
18
+ import errno
19
+ import io
20
+ import os
21
+ from pathlib import Path
22
+ from typing import IO, TYPE_CHECKING, Any, Final
23
+
24
+ from streamlit import env_util, errors
25
+ from streamlit.string_util import is_binary_string
26
+
27
+ if TYPE_CHECKING:
28
+ from collections.abc import Generator
29
+
30
+ # Configuration and credentials are stored inside the ~/.streamlit folder
31
+ CONFIG_FOLDER_NAME: Final = ".streamlit"
32
+
33
+ # If enableStaticServing is enabled, static file served from the ./static folder
34
+ APP_STATIC_FOLDER_NAME: Final = "static"
35
+
36
+
37
+ def get_encoded_file_data(
38
+ data: bytes, encoding: str = "auto"
39
+ ) -> io.StringIO | io.BytesIO:
40
+ """Coerce bytes to a BytesIO or a StringIO.
41
+
42
+ Parameters
43
+ ----------
44
+ data : bytes
45
+ encoding : str
46
+
47
+ Returns
48
+ -------
49
+ BytesIO or StringIO
50
+ If the file's data is in a well-known textual format (or if the encoding
51
+ parameter is set), return a StringIO. Otherwise, return BytesIO.
52
+
53
+ """
54
+ if encoding == "auto":
55
+ # If the file does not look like a pure binary file, assume
56
+ # it's utf-8. It would be great if we could guess it a little
57
+ # more smartly here, but it is what it is!
58
+ data_encoding = None if is_binary_string(data) else "utf-8"
59
+ else:
60
+ data_encoding = encoding
61
+
62
+ if data_encoding:
63
+ return io.StringIO(data.decode(data_encoding))
64
+
65
+ return io.BytesIO(data)
66
+
67
+
68
+ @contextlib.contextmanager
69
+ def streamlit_read(path: str, binary: bool = False) -> Generator[IO[Any], None, None]:
70
+ """Opens a context to read this file relative to the streamlit path.
71
+
72
+ For example:
73
+
74
+ with streamlit_read('foo.txt') as foo:
75
+ ...
76
+
77
+ opens the file `.streamlit/foo.txt`
78
+
79
+ path - the path to write to (within the streamlit directory)
80
+ binary - set to True for binary IO
81
+ """
82
+ filename = get_streamlit_file_path(path)
83
+ if os.stat(filename).st_size == 0:
84
+ raise errors.Error(f'Read zero byte file: "{filename}"')
85
+
86
+ mode = "r"
87
+ if binary:
88
+ mode += "b"
89
+ with open(os.path.join(CONFIG_FOLDER_NAME, path), mode) as handle:
90
+ yield handle
91
+
92
+
93
+ @contextlib.contextmanager
94
+ def streamlit_write(path: str, binary: bool = False) -> Generator[IO[Any], None, None]:
95
+ """Opens a file for writing within the streamlit path, and
96
+ ensuring that the path exists.
97
+
98
+ For example:
99
+
100
+ with streamlit_write('foo/bar.txt') as bar:
101
+ ...
102
+
103
+ opens the file .streamlit/foo/bar.txt for writing,
104
+ creating any necessary directories along the way.
105
+
106
+ path - the path to write to (within the streamlit directory)
107
+ binary - set to True for binary IO
108
+ """
109
+ mode = "w"
110
+ if binary:
111
+ mode += "b"
112
+ path = get_streamlit_file_path(path)
113
+ os.makedirs(os.path.dirname(path), exist_ok=True)
114
+ try:
115
+ with open(path, mode) as handle:
116
+ yield handle
117
+ except OSError as e:
118
+ msg = [f"Unable to write file: {os.path.abspath(path)}"]
119
+ if e.errno == errno.EINVAL and env_util.IS_DARWIN:
120
+ msg.append(
121
+ "Python is limited to files below 2GB on OSX. "
122
+ "See https://bugs.python.org/issue24658"
123
+ )
124
+ raise errors.Error("\n".join(msg))
125
+
126
+
127
+ def get_static_dir() -> str:
128
+ """Get the folder where static HTML/JS/CSS files live."""
129
+ dirname = os.path.dirname(os.path.normpath(__file__))
130
+ return os.path.normpath(os.path.join(dirname, "static"))
131
+
132
+
133
+ def get_app_static_dir(main_script_path: str) -> str:
134
+ """Get the folder where app static files live."""
135
+ static_dir = Path(main_script_path).parent / APP_STATIC_FOLDER_NAME
136
+ return os.path.abspath(static_dir)
137
+
138
+
139
+ def get_streamlit_file_path(*filepath: str) -> str:
140
+ """Return the full path to a file in ~/.streamlit.
141
+
142
+ This doesn't guarantee that the file (or its directory) exists.
143
+ """
144
+ home = Path.home()
145
+ if home is None:
146
+ raise RuntimeError("No home directory.")
147
+
148
+ return str(home / CONFIG_FOLDER_NAME / Path(*filepath))
149
+
150
+
151
+ def get_project_streamlit_file_path(*filepath: str) -> str:
152
+ """Return the full path to a filepath in ${CWD}/.streamlit.
153
+
154
+ This doesn't guarantee that the file (or its directory) exists.
155
+ """
156
+ return str(Path.cwd() / CONFIG_FOLDER_NAME / Path(*filepath))
157
+
158
+
159
+ def get_main_script_streamlit_file_path(main_script_path: str, filename: str) -> str:
160
+ """Return the full path to a file in the .streamlit folder relative to the
161
+ main script's path.
162
+
163
+ This doesn't guarantee that the file (or its directory) exists.
164
+ """
165
+
166
+ return str(
167
+ Path(os.path.abspath(os.path.dirname(main_script_path)))
168
+ / CONFIG_FOLDER_NAME
169
+ / filename
170
+ )
171
+
172
+
173
+ def file_is_in_folder_glob(filepath: str, folderpath_glob: str) -> bool:
174
+ """Test whether a file is in some folder with globbing support.
175
+
176
+ Parameters
177
+ ----------
178
+ filepath : str
179
+ A file path.
180
+ folderpath_glob: str
181
+ A path to a folder that may include globbing.
182
+
183
+ """
184
+ # Make the glob always end with "/*" so we match files inside subfolders of
185
+ # folderpath_glob.
186
+ if not folderpath_glob.endswith("*"):
187
+ if folderpath_glob.endswith("/"):
188
+ folderpath_glob += "*"
189
+ else:
190
+ folderpath_glob += "/*"
191
+
192
+ import fnmatch
193
+
194
+ file_dir = os.path.dirname(filepath) + "/"
195
+ return fnmatch.fnmatch(file_dir, folderpath_glob)
196
+
197
+
198
+ def get_directory_size(directory: str) -> int:
199
+ """Return the size of a directory in bytes."""
200
+ total_size = 0
201
+ for dirpath, _, filenames in os.walk(directory):
202
+ for f in filenames:
203
+ fp = os.path.join(dirpath, f)
204
+ total_size += os.path.getsize(fp)
205
+ return total_size
206
+
207
+
208
+ def file_in_pythonpath(filepath: str) -> bool:
209
+ """Test whether a filepath is in the same folder of a path specified in the PYTHONPATH env variable.
210
+
211
+
212
+ Parameters
213
+ ----------
214
+ filepath : str
215
+ An absolute file path.
216
+
217
+ Returns
218
+ -------
219
+ boolean
220
+ True if contained in PYTHONPATH, False otherwise. False if PYTHONPATH is not defined or empty.
221
+
222
+ """
223
+ pythonpath = os.environ.get("PYTHONPATH", "")
224
+ if len(pythonpath) == 0:
225
+ return False
226
+
227
+ absolute_paths = [os.path.abspath(path) for path in pythonpath.split(os.pathsep)]
228
+ return any(
229
+ file_is_in_folder_glob(os.path.normpath(filepath), path)
230
+ for path in absolute_paths
231
+ )
232
+
233
+
234
+ def normalize_path_join(*args: str) -> str:
235
+ """Return the normalized path of the joined path.
236
+
237
+ Parameters
238
+ ----------
239
+ *args : str
240
+ The path components to join.
241
+
242
+ Returns
243
+ -------
244
+ str
245
+ The normalized path of the joined path.
246
+ """
247
+ return os.path.normpath(os.path.join(*args))
248
+
249
+
250
+ def get_main_script_directory(main_script: str) -> str:
251
+ """Return the full path to the main script directory.
252
+
253
+ Parameters
254
+ ----------
255
+ main_script : str
256
+ The main script path. The path can be an absolute path or a relative
257
+ path.
258
+
259
+ Returns
260
+ -------
261
+ str
262
+ The full path to the main script directory.
263
+ """
264
+ main_script_path = normalize_path_join(os.getcwd(), main_script)
265
+
266
+ return os.path.dirname(main_script_path)