DEV Community

Cover image for You can just turn data scripts into apps!
Aadarsh Kannan
Aadarsh Kannan

Posted on • Updated on

You can just turn data scripts into apps!

Hi there๐Ÿ‘‹! Ever wondered if you can build apps with just a few lines of script/lines of codes. This is where Streamlit comes into play!

About streamlit

Streamlit is an open-source app framework for Machine Learning and data science. It is based on the idea that the best way to learn and use new tools is not through tutorial articles or videos, but by exploring working examples.

In other words, Streamlit is designed to let you build apps for your Machine Learning projects without having to learn a new framework or language. In just a few minutes, you can build an interactive app that lets you explore a dataset, visualize results, and even share your app with others. All you need is basic Python knowledge and a web browser.

Streamlit is built by a team of engineers and data science enthusiasts at Streamlit Inc., a startup based in San Francisco, CA. It is also lightweight and fast. The apps you build with it can be deployed easily on any platform, including Heroku and AWS.

Get Started

Open the terminal from wherever you want to and run the below commands.

$ pip install streamlit
Enter fullscreen mode Exit fullscreen mode
$ streamlit hello
Enter fullscreen mode Exit fullscreen mode

You will be able to see the page as shown below!

Streamlit Homepage

And that's it boom! you are done setting up the framework! Now we can start building whatever the hell we want.

Let's built an app with some basic elements

Create a .py file - This is where we will be writing our code for the app.

First, let's import the streamlit module

import streamlit as st
Enter fullscreen mode Exit fullscreen mode

For this tutorial, let's built a basic page based on Rick and Morty. Below are the codes which you will be able to understand easily. Let's code it out!

# Display text in title formatting
st.title("OOH WEE!") 
Enter fullscreen mode Exit fullscreen mode
# Display text in header and subheader formatting
st.header("Rick and Morty")
st.subheader("American adult animated science fiction sitcom")
Enter fullscreen mode Exit fullscreen mode
# Display an image or list of images
st.image("https://upload.wikimedia.org/wikipedia/commons/9/9e/Rick_and_Morty_title_card.png", caption = "Justin Roiland and Dan Harmon, Public domain, via Wikimedia Commons")
Enter fullscreen mode Exit fullscreen mode
# Write arguments to the app
st.write('**Rick and Morty** is one of the best animated series on television. The show is about the adventures of Rick, a mad scientist, and his grandson, Morty. The series is hilarious and has a lot of heart.')
Enter fullscreen mode Exit fullscreen mode
# Display a code block
code = '''def rick():
     print("Boom! Big reveal! I turned myself into a code block")'''

st.code(code, language='python')
Enter fullscreen mode Exit fullscreen mode
# Display a button widget
if st.button("Just don't click this button"):
     st.image('https://c.tenor.com/5IFS2BehSGUAAAAC/morty-you-dirty-little-doggy.gif')
Enter fullscreen mode Exit fullscreen mode
# Display a slider widget
dimension = st.slider('Select your dimension', 0, 200, 137)
st.write("I'm from C-", dimension, ' dimension')
Enter fullscreen mode Exit fullscreen mode
# Display a select widget
fav = st.selectbox(
     'Who is your favourite character?',
     ('Rick Sanchez', 'Morty Smith', 'Birdperson', 'Summer Smith', 'Mr. Poopybutthole'))
Enter fullscreen mode Exit fullscreen mode
# Display an informational message
st.info('Nobody exists on purpose. Nobody belongs anywhere. We are all going to die. Come lets built apps with Streamlit')
Enter fullscreen mode Exit fullscreen mode

Below is the complete code of our basic app.

import streamlit as st

st.title("OOH WEE!")

st.header("Rick and Morty")

st.subheader("American adult animated science fiction sitcom")

st.image("https://upload.wikimedia.org/wikipedia/commons/9/9e/Rick_and_Morty_title_card.png", caption = "Justin Roiland and Dan Harmon, Public domain, via Wikimedia Commons")

st.write('**Rick and Morty** is one of the best animated series on television. The show is about the adventures of Rick, a mad scientist, and his grandson, Morty. The series is hilarious and has a lot of heart.')

code = '''def rick():
     print("Boom! Big reveal! I turned myself into a code block")'''

st.code(code, language='python')

if st.button("Just don't click this button"):
     st.image('https://c.tenor.com/5IFS2BehSGUAAAAC/morty-you-dirty-little-doggy.gif')

dimension = st.slider('Select your dimension', 0, 200, 137)
st.write("I'm from C-", dimension, ' dimension')

fav = st.selectbox(
     'Who is your favourite character?',
     ('Rick Sanchez', 'Morty Smith', 'Birdperson', 'Summer Smith', 'Mr. Poopybutthole'))

st.write('You favourite character is ', fav)

st.info('Nobody exists on purpose. Nobody belongs anywhere. We are all going to die. Come lets built apps with Streamlit')
Enter fullscreen mode Exit fullscreen mode

Once you've created your script, say rickandmorty.py, the easiest way to run it is with streamlit run:

$ streamlit run rickandmorty.py
Enter fullscreen mode Exit fullscreen mode

You will be able to see a tab in your browser. You can run this in the beginning and build your app by visualizing each element. This will help you understand quickly.

Ooo Wee

Live demo of our app - OOH WEE!

Try experimenting around and build whatever you want.

Need inspiration? Check out what creators from around the world are building - Streamlit Gallery

Deployment

  • Add your app to GitHub
  • Sign up for Streamlit Cloud [with GitHub recommended]

Sign up for Streamlit Cloud.png

  • Log in to share.streamlit.io
  • Once you have signed in to GitHub, you will be taken to your Streamlit Cloud workspace!

Streamlit workspace

  • To deploy an app, click "New app", then fill in your repo, branch, and file path, or click "Paste GitHub URL" and click "Deploy".

deploy_your_app.png

  • You can now watch your app deploy and launch.

deploy_manager.png

And that's it! Your app is live now; just like this live demo and you can share it with others.

Resources ๐Ÿ“š

Conclusion

There are many more possibilities with Streamlit. For example, you can use it to build data exploration apps, dashboard apps, and even games.

Recently Streamlit has launched multipage apps ๐Ÿ“„ and Dashboard analytics ๐Ÿ“Š. It's been evolving so quickly and gaining a lot of popularity among open-source developers. So what are you waiting for? Start building with Streamlit!

sdsf

Happy Streamlit-ing ๐ŸŽˆ

And that's a wrap. Thanks for reading and don't forget to share your feedback. You can find me on Twitter - Aadarsh Kannan. Stay tuned for more!

Stay safe ๐Ÿ˜ท Spread Love โค๏ธ and Keep Exploring ๐Ÿš€

Top comments (0)