top of page

The End of Flask in Data Science

Streamlit Is The Game Changing Python Library That We’ve Been Waiting For


Developing a user-interface is not easy. I’ve always been a mathematician and for me, coding was a functional tool to solve an equation and to create a model, rather than providing the user with an experience. I’m not artsy and nor am I actually that bothered by it. As a result of this, my projects always remained, well, projects. It’s a bit of a problem.


As ones own journey goes, I often need to do a task that’s outside of my domain: usually to deploy code in a manner by which other people can use it. Not even to launch the next ‘big thing’, but just to have my mother, sister or father use a cool little app I built that recommends new places to eat. The answer always required more effort than I desired to put into it, which used to be:

  1. Develop a novel solution (my speciality) — [This I can do]

  2. Design a website using a variety of frameworks that require months of education [This I cannot do]

  3. Deploy code to a server on some web domain [This I can do]

So (2) is where I always lacked motivation because in reality, it wasn’t my speciality. Even if I did find the motivation to deploy some code, the aesthetics of my work would render it unusable.


The problem with using a framework like Flask is that just requires way too much from the individual.

Check this blog here: clearly it’s ridiculous to have to navigate all that just to build a small nifty website that can deploy some code. It would literally take ages.


And that’s why Streamlit is here.


Streamlit

This is where Streamlit comes into its own, and why they just raised $6m to get the job done. They created a library off the back of an existing python framework that allows users to deploy functional code. Kind of similar to how Tensorflow works: Streamlit adds on a new feature in its UI, corresponding to a new function being called in the Python Script.


For example the following 6 lines of code. I append a “title” method, a “write” method, a “select” method and a “write” method (from Streamlit):

import streamlit as st
st.title(‘Hello World’)
st.write(‘Pick an option’)

keys = [‘Normal’,’Uniform’]
dist_key = st.selectbox(‘Which Distribution do you 
want?’,keys)

st.write(‘You have chosen {}’.format(dist_key))

Save that into a file called “test.py”, then run “streamlit run test.py” and it produces the following in your browser on http://localhost:8501 /:

Code above produced this. Fantastic how efficient Streamlits library makes UI programming.


Now this is awesome. It’s both clean to look at and clearly efficient to create.


Jupyter Notebooks is also another successful “alternative” but it’s a bit different. Notebooks is better as a framework for research or report writing however there’s little you can do in the way of actually letting someone else use your code as it’s impractical to give someone else a notebook of code. Co-labs kind of bridges that gap but it’s still not as clean.


Streamlit fills this void by giving the user an ability to deploy code in an easy manner so the client use the product. For those of us who like making small things, this has always been an issue.


Ease of Use

Ok so let’s create something that we may actually want someone else to use.


Let’s say I want to teach my nephew about distributions. I want to make an app that he can use where he selects a distribution, and then it draws a line chart of it. Something as simple as the following:

Code provided below how to create this


In this example, you can see that the user has a choice between 2 items in a drop down menu: and when he selects either, you hope that the line chart would up date with the chart. Taking a step back, I’m providing the user with:

  1. Some Information about a problem

  2. The user then has the ability to make a choice

  3. The corresponding chart is then returned to the user

Now in Flask, something like the above would easily require hundreds of lines of code (before even getting to the aesthetics) however Streamlit have achieved the above in a negligible amount of code. Note that the above required the following ~11 lines of code:

import streamlit as st
import numpy as np

# Write a title and a bit of a blurb
st.title(‘Distribution Tester’)
st.write(‘Pick a distribution from the list and we shall draw the a line chart from a random sample from the distribution’)

# Make some choices for a user to select
keys = [‘Normal’,’Uniform’]
dist_key = st.selectbox(‘Which Distribution do you want to plot?’,keys)

# Logic of our program
if dist_key == ‘Normal’:
    nums = np.random.randn(1000)
elif dist_key == ‘Uniform’:
    nums = np.array([np.random.randint(100) for i in range(1000)])
    
# Display User
st.linechart(nums)

I find it amazing because the amount of code required is so small to produce something that actually looks and works pretty good.

For anyone who’s played around with UI before, you’ll know how difficult it is to achieve something of this quality. To have Streamlit produce an open-source framework for researchers and teams a like, development time has been immensely reduced. I cannot emphasis this point enough.


Given this, no Data Scientist or Machine Learning Researcher can ever complain about not being able to deploy work. Nor can they complain about getting an MVP running. Streamlit have done all the hard work.


Source: Paper.li

0 comments
bottom of page