DEV Community

Cover image for How to use Streamlit :The fastest way to build and share data apps.
Sandeep Balachandran
Sandeep Balachandran

Posted on

How to use Streamlit :The fastest way to build and share data apps.

Hey there 🖐,
Before moving on to the content , take a quick look. 👀

Alt Text

Table of contents

  • 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 🚀🚀 - so let’s get started!

  1. Make sure that you have Python 3.6 or greater installed.

  2. Install Streamlit using PIP and run the ‘hello world’ app:

pip install streamlit
streamlit hello
Enter fullscreen mode Exit fullscreen mode

Create your first Streamlit app

First, we’ll create a new Python script and import Streamlit.

Create a new Python file named app.py, then open it with your IDE or text editor.

Next, import Streamlit

import streamlit as st
# To make things easier later, we're also importing numpy and pandas for
# working with sample data.
import numpy as np
import pandas as pd
Enter fullscreen mode Exit fullscreen mode

Run your app. A new tab will open in your default browser. It’ll be blank for now. That’s OK.

streamlit run app.py
Enter fullscreen mode Exit fullscreen mode

Write a data frame
Along with magic commands, st.write() is Streamlit’s “Swiss Army knife”. You can pass almost anything to st.write(): text, data, Matplotlib figures, Altair charts, and more. Don’t worry, Streamlit will figure it out and render things the right way.

st.title('My first app')
st.write("Here's our first attempt at using data to create a table:")
st.write(pd.DataFrame({
    'first column': [1, 2, 3, 4],
    'second column': [10, 20, 30, 40]
}))
Enter fullscreen mode Exit fullscreen mode

output will be like

theme

Draw charts and maps

Streamlit supports several popular data charting libraries like Matplotlib, Altair, deck.gl, and more. In this section, you’ll add a bar chart, line chart, and a map to your app.

Draw a line chart
You can easily add a line chart to your app with st.line_chart(). We’ll generate a random sample using Numpy and then chart it.

chart_data = pd.DataFrame(
     np.random.randn(20, 3),
     columns=['a', 'b', 'c'])
st.line_chart(chart_data)
Enter fullscreen mode Exit fullscreen mode

theme

Plot a map

With st.map() you can display data points on a map. Let’s use Numpy to generate some sample data and plot it on a map of San Francisco.

map_data = pd.DataFrame(
    np.random.randn(1000, 2) / [50, 50] + [37.76, -122.4],
    columns=['lat', 'lon'])

st.map(map_data)
Enter fullscreen mode Exit fullscreen mode

theme

Show progress

  • When adding long running computations to an app, you can use st.progress() to display status in real time.

First, let’s import time. We’re going to use the time.sleep() method to simulate a long running computation:

import time

Then add the code

left_, right_ = st.beta_columns(2)
latest_iteration = st.empty()
bar = st.progress(0)
pre = left_.button('Start counter')
if pre:
  bar.progress(0)
  for i in range(100):
    # Update the progress bar with each iteration.
    latest_iteration.text(f'Iteration {i+1}')
    bar.progress(i + 1)
    time.sleep(0.1)

Enter fullscreen mode Exit fullscreen mode

Alt Text

Share your app

  • After you’ve built a Streamlit app, it’s time to share it! To show it off to the world you can use Streamlit sharing to deploy, manage, and share your app for free.

  • Streamlit sharing is currently invitation only so request an invite .

It works in 3 simple steps: đŸ„ł

  • Put your app in a public Github repo (and make sure it has a requirements.txt!)

  • Sign into share.streamlit.io

  • Click ‘Deploy an app’ and then paste in your GitHub URL

That’s it! 🎈You now have a publicly deployed app that you can share with the world. đŸ’Ș

Read more about how to make apps - Official docs

Read more about how to share - Official workflow

Thats it for now.

This was my very last post on the year 2020. What an interesting and weired year it has been in all the worst ways lads!!!

So in retrospect, in 2015, not a single person got the answer right to "Where do you see yourself 5 years from now?"

sad

On the bright side, I got to write more contents on this year than any other year.

It is finaly over. I wanted to say thank to each one of you.

thanks

Thanks for reading my contents. If you learned something new from my posts please let me know.

Dont hesitate to share if there any mistakes that i need to correct or topics that i have to research more.

See you next year

Hasta Pronto ! 🙌🙌

Alt Text

Top comments (0)