Streamlit allows you to write Markdown within a Python file (.py
):
import streamlit as st
st.title("Otto Group Product Classification Challenge 🛍")
st.markdown("## 1. Problem Statement")
st.markdown(
"Given a [dataset](https://www.kaggle.com/c/otto-group-product-classification-challenge/overview) with **93 features**, create a **predictive model** which is able to **distinguish between the main product categories**."
)
st.markdown("### 1.2 Evaluation")
st.markdown(
"The evaluation for the competition is **multi-class logarithm loss**. See [Kaggle: Evaluation.](https://www.kaggle.com/c/otto-group-product-classification-challenge/overview/evaluation)"
)
I like that I can write Markdown, but the syntax is cumbersome. You have to wrap your text into st.markdown()
for every line.
Let’s sprinkle in some magic! ✨
Streamlit Magic
Magic commands are a feature in Streamlit that allows you to write markdown and data to your app with very few keypresses.
Let’s see it in action:
# previous code
# imports, etc.
"""
## 3. Prepare Data
Split the data into **training and test set**.
"""
np.random.seed(42)
X = df.drop(["target"], axis=1)
y = df.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
"Data shapes:"
"X_train:", X_train.shape
"X_test:", X_test.shape
"y_train:", y_train.shape
"y_test:", y_test.shape
Now you have a mixture between code and user-facing output in your Streamlit application.