Streamlit Basic Components

Welcome to this blog post about the basic components of Streamlit, a powerful tool for creating interactive web applications with Python. This guide will introduce you to some of the essential components you can use to build your own Streamlit apps.

Table of Contents

  1. Introduction

  2. Text Elements

  3. Data Display Elements

  4. Input Widgets

  5. Media Elements

  6. Layout and Control

  7. Conclusion


Introduction

Streamlit is an open-source Python library that makes it easy to create and share beautiful, custom web apps for machine learning and data science. In just a few minutes, you can build and deploy powerful data apps.


Text Elements

Streamlit offers several functions to display text in your app. Here are some of the basic ones:

FunctionDescriptionExample
st.titleDisplays a titlest.title('This is a title')
st.headerDisplays a headerst.header('This is a header')
st.subheaderDisplays a subheaderst.subheader('This is a subheader')
st.textDisplays fixed-width textst.text('This is some text')
st.markdownDisplays markdown-formatted textst.markdown('**This is bold text**')
st.latexDisplays mathematical expressions formatted in LaTeXst.latex(r'E = mc^2')
st.codeDisplays a block of codest.code('print("Hello, Streamlit!")', language='python')

Data Display Elements

Streamlit provides several ways to display data, including tables and charts:

FunctionDescriptionExample
st.dataframeDisplays a data frame as an interactive tablest.dataframe(my_dataframe)
st.tableDisplays a static tablest.table(my_dataframe)
st.line_chartDisplays a line chartst.line_chart(data)
st.bar_chartDisplays a bar chartst.bar_chart(data)
st.mapDisplays a mapst.map(data)

Input Widgets

Input widgets allow you to capture user input in your Streamlit app:

FunctionDescriptionExample
st.buttonDisplays a buttonif st.button('Click me'): st.write('Button clicked!')
st.sliderDisplays a sliderage = st.slider('Select your age', 0, 100, 25)
st.text_inputDisplays a text input boxname = st.text_input('Enter your name')
st.text_areaDisplays a text areabio = st.text_area('Enter your bio')
st.selectboxDisplays a select boxoption = st.selectbox('Choose one', ['Option 1', 'Option 2', 'Option 3'])
st.multiselectDisplays a multi-select boxoptions = st.multiselect('Choose one or more', ['Option 1', 'Option 2', 'Option 3'])
st.checkboxDisplays a checkboxagree = st.checkbox('I agree')
st.radioDisplays a set of radio buttonschoice = st.radio('Choose one', ['Option A', 'Option B', 'Option C'])

Media Elements

Streamlit allows you to display media like images, audio, and video:

FunctionDescriptionExample
st.imageDisplays an imagest.image('path/to/image.png')
st.audioPlays an audio filest.audio('path/to/audio.mp3')
st.videoPlays a video filest.video('path/to/video.mp4')

Layout and Control

Streamlit offers various elements to manage the layout and flow of your app:

FunctionDescriptionExample
st.sidebarCreates a sidebar for additional input widgetsst.sidebar.selectbox('Choose one', ['Option 1', 'Option 2'])
st.columnsCreates a multi-column layoutcol1, col2 = st.columns(2)
st.expanderCreates an expandable/collapsible sectionwith st.expander('Expand me'): st.write('Hidden text')

Conclusion

This blog post covered the basic components of Streamlit, which you can use to create interactive and user-friendly web applications. Explore these components to enhance your data apps and make them more engaging.

Happy Streamlit-ing!


Feel free to contribute to this repository or ask questions if you need any further assistance.


0
Subscribe to my newsletter

Read articles from Mohamed Abdelwahab directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Mohamed Abdelwahab
Mohamed Abdelwahab

I’m dedicated to advancing healthcare through data science. My expertise includes developing user-friendly Streamlit apps for interactive data exploration, enabling clinicians and researchers to access and interpret insights with ease. I have hands-on experience in advanced data analysis techniques, including feature engineering, statistical modeling, and machine learning, applied to complex healthcare datasets. My focus is on predictive modeling and data visualization to support clinical decision-making and improve patient outcomes.