2022-05-23 00:16:32 +04:00

72 lines
2.4 KiB
Python

# Copyright 2018-2022 Streamlit Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import cast
import streamlit
from streamlit.proto.Empty_pb2 import Empty as EmptyProto
class EmptyMixin:
def empty(self):
"""Insert a single-element container.
Inserts a container into your app that can be used to hold a single element.
This allows you to, for example, remove elements at any point, or replace
several elements at once (using a child multi-element container).
To insert/replace/clear an element on the returned container, you can
use "with" notation or just call methods directly on the returned object.
See examples below.
Examples
--------
Overwriting elements in-place using "with" notation:
>>> import time
>>>
>>> with st.empty():
... for seconds in range(60):
... st.write(f"{seconds} seconds have passed")
... time.sleep(1)
... st.write("✔️ 1 minute over!")
Replacing several elements, then clearing them:
>>> placeholder = st.empty()
>>>
>>> # Replace the placeholder with some text:
>>> placeholder.text("Hello")
>>>
>>> # Replace the text with a chart:
>>> placeholder.line_chart({"data": [1, 5, 2, 6]})
>>>
>>> # Replace the chart with several elements:
>>> with placeholder.container():
... st.write("This is one element")
... st.write("This is another")
...
>>> # Clear all those elements:
>>> placeholder.empty()
"""
empty_proto = EmptyProto()
return self.dg._enqueue("empty", empty_proto)
@property
def dg(self) -> "streamlit.delta_generator.DeltaGenerator":
"""Get our DeltaGenerator."""
return cast("streamlit.delta_generator.DeltaGenerator", self)