streamlit
181 строка · 3.6 Кб
1# Copyright (c) Streamlit Inc. (2018-2022) Snowflake Inc. (2022-2024)
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
15import io16from collections import namedtuple17from dataclasses import dataclass18from datetime import datetime19
20import altair as alt21import graphviz22import matplotlib.pyplot as plt23import numpy as np24import pandas as pd25import plotly.express as px26import pydeck as pdk27from PIL import Image28
29import streamlit as st30
31np.random.seed(0)32
33st.subheader("st.write(markdown)")34
35st.write("Hello", "World")36
37st.write("This **markdown** is awesome! :sunglasses:")38
39st.write("This <b>HTML tag</b> is escaped!")40
41st.write("This <b>HTML tag</b> is not escaped!", unsafe_allow_html=True)42
43
44class ClassWithReprHtml:45def _repr_html_(self):46return "This <b>HTML tag</b> is also not escaped!"47
48
49st.write(ClassWithReprHtml())50st.write(ClassWithReprHtml(), unsafe_allow_html=True)51
52st.write(100)53
54st.write(None)55
56st.write(datetime(2021, 1, 1))57
58st.write(np.float64(1.0))59
60
61class SomeObject1(object):62def __str__(self):63return "1 * 2 - 3 = 4 `ok` !"64
65
66st.write(SomeObject1()) # escaped single line string67
68
69class SomeObject2(object):70def __str__(self):71return "1 * 2\n - 3\n ``` = \n````\n4 `ok` !"72
73
74st.write(SomeObject2()) # escaped multiline string75
76string_io = io.StringIO()77string_io.write("This is a string IO object!")78
79st.write(string_io)80
81
82def stream_text():83yield "This is "84yield "streamed text"85
86
87st.subheader("st.write(generator)")88
89st.write(stream_text)90
91st.write(stream_text())92
93
94st.subheader("st.write(dataframe-like)")95
96st.write(pd.DataFrame(np.random.randn(25, 3), columns=["a", "b", "c"]))97
98st.write(pd.Series([1, 2, 3]))99
100st.write(101pd.DataFrame(np.random.randn(25, 3), columns=["a", "b", "c"]).style.format("{:.2%}")102)
103
104st.write(np.arange(25).reshape(5, 5))105
106st.subheader("st.write(json-like)")107
108st.write(["foo", "bar"])109
110st.write({"foo": "bar"})111
112st.write(st.session_state)113st.write(st.experimental_user)114st.write(st.query_params)115
116Point = namedtuple("Point", ["x", "y"])117st.write(Point(1, 2))118
119st.subheader("st.write(help)")120
121st.write(st.dataframe)122
123
124@dataclass
125class ExampleClass:126name: str127age: int128
129
130st.write(ExampleClass)131
132st.subheader("st.write(exception)")133
134st.write(Exception("This is an exception!"))135
136st.subheader("st.write(matplotlib)")137
138fig, ax = plt.subplots()139ax.hist(np.random.normal(1, 1, size=100), bins=20)140
141st.write(fig)142
143st.subheader("st.write(altair)")144
145df = pd.DataFrame(np.random.randn(50, 3), columns=["a", "b", "c"])146chart = alt.Chart(df).mark_circle().encode(x="a", y="b", size="c", color="c")147st.write(chart)148
149st.subheader("st.write(plotly)")150
151fig = px.scatter(df, x="a", y="b")152st.write(fig)153
154st.subheader("st.write(graphviz)")155
156graph = graphviz.Digraph()157graph.edge("run", "intr")158graph.edge("intr", "runbl")159graph.edge("runbl", "run")160
161st.write(graph)162
163# Simple pydeck chart:
164
165st.subheader("st.write(pydeck)")166
167st.write(168pdk.Deck(169map_style=None,170initial_view_state=pdk.ViewState(171latitude=37.76,172longitude=-122.4,173zoom=11,174pitch=50,175),176)177)
178
179st.subheader("st.write(Image)")180
181st.write(Image.new("L", (10, 10), "black"))182