Table of Contents
Note: I’m assuming you already have streamlit installed and already know a little bit on how to work with it.
There are four essential components required to launch your streamlit application on Heroku.
mkdir -p ~/.streamlit/
echo "\
[server]\n\
headless = true\n\
port = $PORT\n\
enableCORS = false\n\
\n\
" > ~/.streamlit/config.toml
Note: You do not need to name this file exactly setup.sh, it can be anything however it needs to end with an .sh file extension. However, naming it setup.sh seems to be the norm.
This file essentially lists all the specific python plugins we will use in our streamlit application. It may look like this.
newspaper3k==0.2.8
streamlit==0.61.0
nltk==3.4.5
Note: Name this file as Procfile, do not put any extensions after it. It is a standard text file.
web: sh setup.sh && streamlit run your_application.py
The time consuming part is building your streamlit application, and this is stored as a python script.
For this tutorial, I made a simple python application based on a medium post on (web scraping articles online)[https://towardsdatascience.com/the-easy-way-to-web-scrape-articles-online-d28947fc5979].
# Important Plugins
import streamlit as st #start off by loading streamlit
import nltk
nltk.download('punkt')
# importing Newspaper3k
import newspaper
from newspaper import Article
# Custom Functions
#---------------------------------
#article downloader
def run_api(user_url_input):
article = Article(user_url_input)
article.download()
article.parse()
article.nlp()
return meta_extraction(article)
#article meta extraction + summarizer
def meta_extraction(article):
st.write('**The title of this article is ** ' + article.title)
st.write('**The author of this article is **' + str(article.authors))
st.write('**The published date of this article is **' + str(article.publish_date))
st.write('**The source of this article is **' + article.source_url)
st.write('## Top Keywords of this Article Below:')
st.write(str(article.keywords))
st.write('## Summary of Article Below:')
st.write(article.summary)
return
# Page Design
#-------------------------------
st.markdown('# This Streamlit application will scrape, display some meta data, and summarize articles.')
# Text Input
user_url_input = st.text_input("Please enter the URL of article")
# Button to Run the script
if st.button('Summarize the Article'):
st.write(run_api(user_url_input))
else:
st.write(st.markdown('**Button not yet executed.**'))
Before we continue, make sure all those files are in the same directory.
I’m assuming you guys know how to sign up for a Heroku account.
Note: Before we begin with working with Heroku on the terminal, I suggest navigating to the folder where the files from Part 1 of this tutorial is located.
$ heroku login
You will be redirected to a web page where you can login, or if you are already logged in on your web browser, it will automatically log in to your account that you made.
Running the code below will automatically create a brand new Heroku application with a random name.
$ heroku create
or you could give it your own name…
$ heroku create name_of_heroku_instance
$ git push heroku master
This part may take a bit of time because the Heroku application is setting things up and installing the required python plugins on the server. Once, completed it should output this message: remote: Verifying deploy… done.
$ heroku open
Voila, there you go! If you followed all the steps above and copied all the code. Your streamlit application on heroku should look like this below.
Sometimes you may forget that you already created a remote heroku application in the folder you are working on.
To check, run this code.
$ git remote -v
If it is different, remove this remote connection
$ git remote rm heroku
Then add the correct heroku address that you want your application to be hosted on.
$ git remote add heroku (git.heroku.com/heroku_application.git)
Maybe, you don’t really want to push your application files through your terminal. An alternative method is through Github. However, this needs to be done through the Heroku Dashboard on your chosen web browser.
From here you can click on ‘View’ to see your app.
I would also suggest enabling automatic deploys under the Automatic deploys section. This will automatically push the latest master branch from your GitHub whenever it is updated with a new version.
There you go, two ways to deploy your streamlit application on Heroku.