Will Streamlit kill off Flask?

Ok, the title is a little dramatic. How about … Streamlit will replace a lot of Flask-based applications.

Can Flask be on the endangered technology list?

At the organization I work for, we are well on our way to convert Flask-based micro-services to Streamlit-based micro-services.

My productivity has increased ten-fold in going to Streamlit. Admittedly, this a little hard to measure now, as all the micro-services I am refactoring are currently in Flask, feeding internal dashboards.

My productivity, designing, and deploying micro-services will probably become more significant as I get more experienced with Streamlit with future Streamlit releases.

Luckily, I work for a company whose management appreciates the rapid evolution of new open-source packages and lets me try them on services (but it has to prove better) that are not broke. These Flask applications, especially dashboards, are relatively more expensive to change and maintain.

I will use Docker so that we can compare Flask to Streamlit and my examples reproduced on your platform.

Faster Gratification

You can skip steps 1. through 4. and go directly to step 5 and 6. by accomplishing in the directory of your choosing:

git clone https://github.com/bcottman/webApps.git

The above operation will create /webAppdirectory tree with all the code shown is steps 1–4. However, you can also get the code by cut/paste from this article. Use git clone and save yourself some time.

1. Creating Directory Tree

We can start by creating a directory tree to hold associated files.

# I start at subdirectory ˜/PROJECTS
# You can create directory tree anywhere you want
mkdir webApps; cd webApps; mkdir Flask; mkdir Streamlitcd Flask; mkdir helloWorld; touch Dockerfile
touch helloworld.py; touch requirements.txt
touch README.md (optional)
cd Streamlit; mkdir helloWorld; touch Dockerfile
touch helloworld.py; touch requirements.txt
touch README.md (optional)

2a.Create Flask Dockerfile in webApp/Flask

FROM python:3.7
WORKDIR /helloWorld
COPY requirements.txt ./requirements.txt
RUN pip3 install -r requirements.txt
COPY . .
CMD python helloWorld.py

2b. Create Streamlit Dockerfile in webApp/Streamlit

FROM python:3.7
WORKDIR /helloWorld
COPY requirements.txt ./requirements.txt
RUN pip3 install -r requirements.txt
COPY . .
CMD streamlit run helloWorld.py

3.a Create Flask requirements.txtin webApp/Flask

Flask

3.b Create Streamlit requirements.txtin webApp/Streamlit

streamlit

So far, there seems little difference between Flask-Docker and Streamlit-Docker setup.

4a. Implementing FlaskhelloWorld.pyin webApp/Flask

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello():
return "Hello World!"

if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0')

4b. Implementing StreamlithelloWorld.pyin webApp/Streamlit

import streamlit as st

st.write("Hello World!!!")

There is a some difference between and Flask helloWorld.py Streamlit helloWorld.py.The former is seven lines long and the latter is two lines long.

5a. Build Docker-Flask Hello World image

$ docker build -t hello-world-flask:latest . 

output[=>]:

Sending build context to Docker daemon 5.12kB
Step 1/8 : FROM ubuntu:latest
— -> 775349758637
Step 2/8 : RUN apt-get update -y
— -> Using cache
— -> 5b94628555d9
Step 3/8 : RUN apt-get install -y python-pip python-dev build-essential
— -> Using cache
— -> 09251e029a96
Step 4/8 : COPY . /app
— -> Using cache
— -> 02582d71cc30
Step 5/8 : WORKDIR /app
— -> Using cache
— -> caa354f9de54
Step 6/8 : RUN pip install -r requirements.txt
— -> Using cache
— -> b20cfd971e31
Step 7/8 : ENTRYPOINT [“python”]
— -> Using cache
— -> bb68183e8592
Step 8/8 : CMD [hello-world.py”]
— -> Using cache
--> cd9436cda25f
Successfully built cd9436cda25f
Successfully tagged hello-world-flask:latest

Since I already built the hello-world-flask image earlier, Docker is optimizing by using it’s cache. When you first build the image, it probably will range from 0.2–3 minutes, depending mostly on what it has to download and your CPU speed. If you build again, it will be in Docker cache and probably require less than 20 seconds to build.

5b. Build Docker-Streamlit helloWorld.py World image

$ docker build -f Dockerfile -t helloworld-streamlit:latest .

6b. Run Docker-Flask helloWorld.py World image

$ docker run -p 5000:5000 helloworld-flask

output[=>]:

* Serving Flask app "app" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.* Debug mode: on
* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 126-100-538
172.17.0.1 - - [19/Dec/2019 16:58:04] "GET / HTTP/1.1" 200 -
172.17.0.1 - - [19/Dec/2019 16:58:04] "GET /favicon.ico HTTP/1.1" 404 -
Output from Flask helloWorld.py

6b. Run Docker-Streamlit helloWorld.py World image

docker run -p 8501:8501 helloworld-streamlit &

output[=>]:

You can now view your Streamlit app in your browser.
Network URL: locahost:8501
External URL:<your External URL>

Your message maybe different, but you see Streamlit results in your browser on URL localhost:8051.

Output from Streamlit helloWord.py

Problems

If you get this message, you need to start or restart Docker. I am on a Mac, so I restart Docker Desktop.

Cannot connect to the Docker daemon at Unix:///var/run/docker.sock. Is the docker daemon running?

Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?.

References

Some references for Streamlit include:

Summary

In this article, we compared Flask to Streamlit using the simple hello World! example. We found that Flask needed seven lines of code while Streamlit needed two lines of code.

We put both applications in a Docker container whose environment was Python 3.7. Either one can is used as a production micro-service on a local web service or your cloud provider.

This Part 1 article showed a very simple micro-service. In Part 2, we will develop together a much more complicated micro-service that has input, interactive table display, and plotting.

In Part 2, we will need to be a full-stack Flask developer who needs to know, at least, how to use Javascript, HTML, CSS, and JSON.

If you are a full-stack Streamlit developer, you need NOT know Javascript, HTML, CSS, JSON, and different POST/GET URL packages in the stack. Instead, your stack will consist of Streamlit (and maybe Docker). That is it!

I hope you have this simple example of Part 1 useful. I am looking forward to Part 2 of this series, and I hope you are also!

The Startup

Get smarter at building your thing. Join The Startup’s +787K followers.

Sign up for Top 10 Stories

By The Startup

Get smarter at building your thing. Subscribe to receive The Startup's top 10 most read stories — delivered straight into your inbox, once a week. Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Bruce H. Cottman, Ph.D.

Written by

Physicist, Machine Learning Scientist and constantly improving Software Engineer. I extrapolate the future from emerging technologies.

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +787K followers.

Bruce H. Cottman, Ph.D.

Written by

Physicist, Machine Learning Scientist and constantly improving Software Engineer. I extrapolate the future from emerging technologies.

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +787K followers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store