Creating Shareable Data Apps Using Streamlit and AWS EC2
Hosting a Streamlit app on AWS EC2

Creating Shareable Data Apps Using Streamlit and AWS EC2

I came across Streamlit last week while looking for solution to host python apps online so that I can share with my network. The Framework boasts of being the easiest and the fastest way of creating interactive apps, and after spending just a few hours creating an interactive resume, I can vouch for that.

Streamlit provides an extensive documentation and tutorials to help create applications which can be found here. They also have a growing community of contributors, which is super responsive. So, if you have a question, shoot right away.

In this tutorial we will not go in detail on how to create a Streamlit app, but rather host it on AWS EC2 so that you can share it with the world. Lets get started.

  • Create a python code file "demoapp.py" with the following code. You can also write your own custom code for the app.
## Demo Streamlit app on EC2


import streamlit as st
import plotly as py
import plotly.io as pio
from plotly import graph_objects as go




labels =  ["People who like streamlit", "People who don't know about streamlit"]
values = [80, 20]


# pull is given as a fraction of the pie radius
fig = go.Figure(data=[go.Pie(labels=labels, values=values,textinfo='label+percent',
                             insidetextorientation='radial', pull=[0, 0, 0.2, 0] #,hole = 0.2
                            )])


fig.update_layout(
    title_text="Demo streamlit app")


st.plotly_chart(fig, sharing='streamlit')
  • Save this file to your preferred folder
  • Next open command prompt and check whether you have Streamlit installed. You can do that by typing "streamlit --version" and pressing enter.
No alt text provided for this image
  • If not found just type "pip install streamlit" and hit enter. This will install Streamlit into your system
  • Now go to the directory where the "demoapp.py" file is saved
  • Then type "streamlit run demoapp.py" and hit enter
Running a Streamlit app

Voila, a new window will open on your browser with the app, if not you can click on either on the Local URL or Network URL to access the app.

No alt text provided for this image


You can use the Network URL to display the app on a Smart TV or a secondary device and see real time changes you make to the app. Pretty cool, right?

Now that you have created a really cool app, you'd want to share it with your friends and peers.

There are several options to host the app online like AWS, Heroku, Google app engine, Azure, etc. We will discuss how to host it on an AWS EC2 instance.

AWS offers a string of free services for budding developers to play around. We will be using Amazon Elastic Compute Cloud (Amazon EC2).

An EC2 instance is a virtual server in Amazon's Elastic Compute Cloud (EC2) for running applications on the Amazon Web Services (AWS) infrastructure

I am assuming that you have an AWS account, if not just go to link, and create a free account. You will be asked for your credit card details, but don't worry, you won't be charged if you keep your usage limited to the free tier.

  • After logging to AWS console click on Services tab and choose EC2.
AWS management console
  • On the next screen find "Launch Instance" and click it.
No alt text provided for this image
  • Choose an "Ubuntu Server" Amazon Machine Image.
No alt text provided for this image
  • Select the "Free tier eligible" instance type
No alt text provided for this image
  • Click "next: Configure Instance Details". You don't need to change anything on this page.
  • Click "next: Add Storage". You can leave the default options be on this page too.
  • Click "next: Add Tags". You can add tags on this page if you want.
  • Now comes the important part : "next: Configure Security Group". Create a custom TCP rule with Port '8501' and Source 'Anywhere'.
No alt text provided for this image
  • Click "Review and Launch" and then on the next page "Launch". You will get a pop up to Select a Key Pair. Select "Create a new key pair" and give it a name and download it. Click the "Launch Instance" button and the instance will be running in a couple of minutes
No alt text provided for this image
  • Go to EC2 dashboard, right click on the running instance and click "connect".
No alt text provided for this image
  • A pop up will open with connection details. Copy the green marked line.
No alt text provided for this image
  • Open your command prompt, go to the folder where the key is saved. Use the copied command and press enter. You will get a prompt asking, whether you want to continue connecting, type "Yes" and press enter.
No alt text provided for this image
  • This instance has python 3.6.9 and Git installed by default. We will be using Git to upload our app files to the instance.
  • You need to create a repository on Git and upload the files. Then find the "Clone with HTTPS link and copy to clipboard".
No alt text provided for this image


  • Type "git clone <copied link>" in command prompt and press enter. Your git repository will be cloned. You can check whether its done using ls command.
No alt text provided for this image
  • Now we need to install Streamlit on the virtual machine. Use the following commands to do that.

Edit:We also need to install plotly, as it is required for our app. You might need to install other dependencies if required by the app.

sudo apt update <Enter>

sudo apt-get install python3-pip <Enter> 

sudo su <enter>

pip3 install streamlit<Enter>

pip3 install plotly <Enter>

exit <Enter>



Now we are all set to run our Streamlit app

  • Go to the "demoapp" directory and run the app by typing "streamlit run demoapp.py" and pressing enter.
No alt text provided for this image

Now your app is hosted online, you can share the External URL with anyone who has a working internet connection and they will be able to access your app.


Streamlit automatically scales the applications for different devices.


Streamlit hosted app


Now this might seem like a really long tutorial, but if you follow the entire process, it hardly takes more than 10 minutes, and you got yourself a fully functional app running online.

I also hosted my resume online using the same steps. Check out the git repository here.

Next steps:

I hosted the resume app two weeks ago and one thing I found was that closing the command prompt kills your app. Check out this tutorial to prevent this.

Also, instead of sharing the External IP with everyone, if you want to give your app a user friendly name you can use AWS Route 53. Check out the tutorial to do this<Coming soon>

Final thoughts:

Streamlit is a really simple and fast way to create interactive applications, and using this tutorial you can share the applications with the world. Streamlit supports most ML and Data Analysis libraries and more are added every day.

No alt text provided for this image

Do try it out, and reach out if you have any questions.

Steve Newman

Data Scientist | Business Intelligence Analyst

3 年

Hi Abhishek Gupta, I followed the guide and was successful with one exception. It only runs on port 8501, in my case cigars-rec.com:8501. How do you get it to run on the default port which I believe is 80. For example, the app run on cigars-rec.com rather than cigars-rec.com:8501? Thanks!

回复
Vaibhav Bhatia

Data Science Consultant | ZS

4 年

Great tutorial Abhishek Gupta. Was following it hands on but got an error where the default python version used by the ubuntu instance was 2.7. I installed python 3.6 but still the ubuntu instance is using 2.7 by default due to which I am not able to run streamlit on ec2. Did you get any such error ? if yes, how did you resolve it?

回复
Ricardo Sueiras

Helping developers experience the future now

4 年

I also only heard about Streamlit last week so found your post helpful to deeper understand it. Thanks!

Abhishek Pawar

Senior Data Scientist | Blogging @ Medium

4 年

I was thinking of doing the same! Thanks for sharing amazing info!

Ritika Singh

Senior Manager, Advanced Analytics at EXL | Data Analytics & Engineering | Product Management

4 年

This is quite detailed.. Great Job on putting this together!

要查看或添加评论,请登录

社区洞察

其他会员也浏览了