Streamlit Spaces
Last updated
Last updated
Streamlit gives users freedom to build a full-featured web app with Python in a reactive way. Your code is rerun each time the state of the app changes. Streamlit is also great for data visualization and supports several charting libraries such as Bokeh, Plotly, and Altair. Read this about building and hosting Streamlit apps in Spaces.
Selecting Streamlit as the SDK when will initialize your Space with the latest version of Streamlit by setting the sdk
property to streamlit
in your README.md
file’s YAML block. If you’d like to change the Streamlit version, you can edit the sdk_version
property.
To use Streamlit in a Space, select Streamlit as the SDK when you create a Space through the . This will create a repository with a README.md
that contains the following properties in the YAML configuration block:
Copied
You can edit the sdk_version
, but note that issues may occur when you use an unsupported Streamlit version. Not all Streamlit versions are supported, so please refer to the to see which versions are available.
For in-depth information about Streamlit, refer to the .
In the following sections, you’ll learn the basics of creating a Space, configuring it, and deploying your code to it. We’ll create a Hot Dog Classifier Space with Streamlit that’ll be used to demo the model, which can detect whether a given picture contains a hot dog đźŚ
You can find a completed version of this hosted at .
We’ll start by and choosing Streamlit as our SDK. Hugging Face Spaces are Git repositories, meaning that you can work on your Space incrementally (and collaboratively) by pushing commits. Take a look at the guide to learn about how you can create and edit files before continuing.
Copied
The Spaces runtime will handle installing the dependencies!
To create the Streamlit app, make a new file in the repository called app.py, and add the following code:
Copied
You can use the HTML <iframe>
tag to embed a Streamlit Space as an inline frame on other webpages. Simply include the URL of your Space, ending with the .hf.space
suffix. To find the URL of your Space, you can use the “Embed this Space” button from the Spaces options.
For example, the demo above can be embedded in these docs with the following tag:
Copied
Please note that we have added ?embed=true
to the URL, which activates the embed mode of the Streamlit app, removing some spacers and the footer for slim embeds.
id
is set to <iframe />
that is used to specify the auto-resize target.
The iFrame Resizer
is loaded via the script
tag.
The iFrameResize()
function is called with the ID of the target iframe
element, so that its size changes automatically.
Copied
For the Hot Dog Classifier we’ll be using a 🌍 to use the model, so we need to start by installing a few dependencies. This can be done by creating a requirements.txt file in our repository, and adding the following dependencies to it:
This Python script uses a to load the model, which is used by the Streamlit interface. The Streamlit app will expect you to upload an image, which it’ll then classify as hot dog or not hot dog. Once you’ve saved the code to the app.py file, visit the App tab to see your app in action!
Streamlit has supported automatic iframe resizing since so that the size of the parent iframe is automatically adjusted to fit the content volume of the embedded Streamlit application.
It relies on the library, for which you need to add a few lines of code, as in the following example where
We can pass options to the first argument of iFrameResize()
. See for the details.
Additionally, you can checkout .