Choosing the Right Tool for Your Next Data Science Web Application
With the advanced computing power of the modern age, we can do more with data than ever before. It's possible to design and implement potentially life-changing technologies from the comfort of your home, using just your laptop and the immense amount of data available via the internet.
But there's a catch. An impressive machine learning model or insightful data visualization tool is of little use to anyone if it's just sitting on your computer as a code file. To really have an impact, you need to be able to share your inventions with other people in a compact, usable way.
The traditional way to do this is through a web application. By taking your Data Science tool, turning it into an application, and hosting it on a server accessible to anyone via the internet, you can immediately make your tool accessible to millions of people.
Unfortunately, this is not as simple as it sounds. Building a web application is a fairly complex programming task, and it involves a set of skills beyond the expertise of many data scientists. Coding one from scratch would be a challenge indeed.
Luckily, many companies are in the business of web frameworks. A web framework is essentially an extensive library which provides the foundational structure for building a web application, making it significantly easier to get started.
In this article, I will discuss the advantages and drawbacks of three well-established web frameworks in Python: Flask, Django, and Streamlit. It can be difficult to know which tool to use if you're just getting started, and my hope is to help you determine which is best for your skill level and overall goal. Note that this article does not discuss the specifics of how to actually build an application with these tools.
Let's get into it, shall we?
Flask
Flask [1] is often the first framework introduced to people who are new to web development. You'll often hear it described as extremely lightweight and fairly easy to learn if you already know Python. Here, the term "lightweight" means that Flask does not have any external dependencies, and it provides the most basic tools needed to build a web application without overwhelming you with features you might not need.
Flask is so lightweight that it is often referred to as a "micro" framework. You can start with the simplest possible application, and pick more features as needed. Furthermore, it is straightforward enough that if you already know Python, you won't feel like you're learning a brand new language.
However, this simplicity is a double-edged sword. Because Flask is lightweight, it also does fewer things for you automatically. In other words, as you are building up your web application, you will have to figure out quite a few things on your own.
I've never used Flask myself, but I will admit that it has been recommended to me many times. The benefits seem to outweigh the costs in the eyes of the general Programming community.
Let's review before moving on.
The Pros
- Extremely lightweight (a "micro" framework, in some sense)
- You can choose the features you need
- Arguably easier to learn as a result of the above two qualities
The Cons
- Not as many built-in features
- Will need to implement many aspects of your web application on your own as a result of the above
Django
This is my personal framework of choice. I only recently got into full-fledged Web Development, and previously settled for simpler alternatives (see Streamlit section below). I had to choose between Django and Flask, and I ended up deciding that Django fit my needs better.
Django is the complete opposite of Flask in that it is rather heavyweight. It has an enormous set of features – and when you build a Django project, you get them all, whether or not you need them. This has positive and negative aspects.
On the upside, you will have to implement fewer things from scratch, since so much functionality is built in. One great example of this is the Django admin page. All Django projects automatically implement a database admin webpage which allows a "superuser" to add and remove data without writing any code. This can be really useful if you want to design a web application for a team of people where one person needs to manage the associated database, but doesn't have the requisite programming skills.
On the other hand, the projects are large, complex, and can be intimidating since you get everything all at once. The simplest Flask application can be written in one code file, but any Django project will automatically make a huge amount of directories and associated files the moment you start.
Django also had really detailed documentation and excellent tutorials [3] written by the developers themselves. For some, this is a double-edged sword, since the developers are quite opinionated. So, when you do something in Django, you have to do it the "Django" way. However, this is unlikely to be an issue if web development isn't your primary programming strength and you're just looking to get started.
So, in summary –
The Pros
- Comes with many built-in features
- The Django admin is an incredible addition you get without any extra work
- Django offers a thorough set of tutorials for getting started that the company itself wrote (ensuring the information is accurate and adheres to best practices)
The Cons
- Very heavyweight – you get all the features as part of a Django project, whether you want them or not
- Django's developers have strong opinions about web development that are reflected in the way things must be done when programming in Django. If you disagree with these opinions, you may find Django frustrating.
Streamlit
"Streamlit [4] turns data scripts into shareable web apps in minutes. All in pure Python. No front‑end experience required."
The above quote is taken directly from Streamlit's front page, and is a pretty solid summary. Streamlit was initially designed to allow data scientists who lacked web programming skills to deploy their data science tools with ease to the internet.
This is Streamlit's greatest advantage. You can write your data science tool in Python, make a few syntax changes to conform to Streamlit's specifications, add some header lines of code, and your application is ready to go! Well, locally at least – though Streamlit also offers cloud support as a next step.
Streamlit also has some nice built-in features (sliders, buttons, and other widgets) that can be used to give your application a nice boost.
However, Streamlit also has two disadvantages. The obvious one is that you're effectively restricted to Streamlit's default interface, so you can't really customize your application's appearance in any way. Streamlit isn't designed for personalization; it simply provides a quick way to get a tool out there and collect data if needed.
Additionally, Streamlit has some issues dealing with large data sets. In my experience (and the experience of other folks I have worked with), running potentially expensive computational processes (such as rendering visualizations, for instance) can lag a fair bit in Streamlit. This anecdotal experience is supported by discussion on the Streamlit forum [5].
Overall, Streamlit is fast and easy to learn, but not nearly as flexible as Flask and Django.
The Pros
- Designed for data scientists
- Requires minimal knowledge of web development
- Can be used to build and deploy a web application extremely quickly
The Cons
- Very little freedom in terms of customizing your web application
- Potential efficiency issues with large data sets or expensive operations
Final Thoughts
Choosing the right framework is both a matter of personal preference as well as your exact needs. I can't speak to your preferences, so I will talk briefly on the second point to conclude.
Want to just test out a quick tool and don't have much time to put it together? Streamlit is likely your best bet. Trying to learn web development and advance your Python skills? Go with Flask or Django. Both can work fine for large, involved apps, so this is where personal preference comes back into play.
As with all programming, there is only so much you can get from an article. At this point, I'd encourage you to get out there and try each one for yourself. I'd love to hear your thoughts and reflections in the comments.
Happy web developing!
Want to excel at Python? Get exclusive, free access to my simple and easy-to-read guides here. Want to read unlimited stories on Medium? Sign up with my referral link below!
References
[1] https://flask.palletsprojects.com/en/2.2.x/ [2] https://www.djangoproject.com/ [3] https://docs.djangoproject.com/en/4.1/intro/tutorial01/ [4] https://streamlit.io/ [5] https://discuss.streamlit.io/t/whether-streamlit-can-handle-big-data-analysis/28085/15