How to add an overwritable single element container in Streamlit

In this recipe, we will learn how to add an overwritable single element container in Streamlit in a simple manner with the help of an example.

Recipe Objective: How to add an over writable single element container in Streamlit?

"st.empty" allows you to add a single element container. You can add over writable elements using the "with" command. You can replace the elements several times and even clear the elements altogether. Take a look at the following sample code to understand how "st.empty" can be used.

Explore the Real-World Applications of Recommender Systems

Code:

#importing required libraries
import streamlit as st
import time

#creating a simple 30 seconds countdown using st.empty
with st.empty():
i=30
while i>0:
st.write(f"{i} seconds left")
time.sleep(1)
i=i-1
st.write("Time's up!!") #text to display after the countdown ends


#creating a single element placeholder
placeholder = st.empty()

#adding some text into the placeholder
placeholder.text("Initial text")

#replacing the initial text with multi-elements
with placeholder.container():
st.write("This is element 1")
st.write("This is element 2")

#clearing all the elements
placeholder.empty()

To run the app, either create an appname.py file with the above code using any text editor, or if you are using a jupyter notebook, you need to download your .ipynb notebook as a Python (.py) file and run the same using the "streamlit run appname.py" command. Once you run the command, the app will automatically open in your default browser.

What Users are saying..

profile image

Savvy Sahai

Data Science Intern, Capgemini
linkedin profile url

As a student looking to break into the field of data engineering and data science, one can get really confused as to which path to take. Very few ways to do it are Google, YouTube, etc. I was one of... Read More

Relevant Projects

Build Regression (Linear,Ridge,Lasso) Models in NumPy Python
In this machine learning regression project, you will learn to build NumPy Regression Models (Linear Regression, Ridge Regression, Lasso Regression) from Scratch.

Build an AI Chatbot from Scratch using Keras Sequential Model
In this NLP Project, you will learn how to build an AI Chatbot from Scratch using Keras Sequential Model.

MLOps Project to Deploy Resume Parser Model on Paperspace
In this MLOps project, you will learn how to deploy a Resume Parser Streamlit Application on Paperspace Private Cloud.

Build a Customer Churn Prediction Model using Decision Trees
Develop a customer churn prediction model using decision tree machine learning algorithms and data science on streaming service data.

Hands-On Approach to Master PyTorch Tensors with Examples
In this deep learning project, you will learn how to perform various operations on the building block of PyTorch : Tensors.

Build a Logistic Regression Model in Python from Scratch
Regression project to implement logistic regression in python from scratch on streaming app data.

Ola Bike Rides Request Demand Forecast
Given big data at taxi service (ride-hailing) i.e. OLA, you will learn multi-step time series forecasting and clustering with Mini-Batch K-means Algorithm on geospatial data to predict future ride requests for a particular region at a given time.

ML Model Deployment on AWS for Customer Churn Prediction
MLOps Project-Deploy Machine Learning Model to Production Python on AWS for Customer Churn Prediction

Abstractive Text Summarization using Transformers-BART Model
Deep Learning Project to implement an Abstractive Text Summarizer using Google's Transformers-BART Model to generate news article headlines.

Build a Hybrid Recommender System in Python using LightFM
In this Recommender System project, you will build a hybrid recommender system in Python using LightFM .