Build a Web App with Flask in Python

I recently decided that I wanted to build a web app. After googling “languages for web development”, as you can imagine this resulted in a countless number of options. Java was one of the most common languages along with PHP, C++, etc. But one thing that caught my eye was Python. I thought that since I was already comfortable with Python that I would investigate more.

Django and Flask seemed to be the most common of these Python-based web app languages and after a little more research it seemed like Flask might be the better fit for me.

In this post I’m going to explain a little bit about what Flask is and how you can get started with it. One thing to note however, in order to create any web app you have to be somewhat familiar with HTML and JavaScript. Before starting this I was not and it took me a little while to get up to speed, but once I did it was fairly simple to incorporate into my program. Although I won’t be going into too much detail here about HTML and JS, I will create a separate post that talks specifically about these.

Withing the Flask framework, you can also use any kind of standard Python code. So what makes this language so great is that if you want to create an app that uses Machine Learning for example, all of this can be done in one piece of code.

In our example, we will be doing basic Natural Language Processing.

Using Flask in Python is as simple as just importing some libraries. Below are some of the most common libraries including: flask, flask_wtf, wtforms, and werkzeug.

We can import all the rest of the Python libraries we’ll need for the NLP piece of our app.

Next we’ll start building out the framework of our app. The first line of app= Flask (__name__) is very important because once the code is loaded to the server, everything in between this and the if name == ‘main’: app.run() is what will be running the app.

The @app.route(‘/’) calls a specific page denoted by the def. So in our example below at the main home page (denoted by ‘/’), we are declaring it as index. We are also denoting that we want the user to log in (user_logged_in= True). Finally the return render_template calls a specific HTML template we created, in this case main.html.

There are many different kinds of templates you can use, from account log in to specific types of forms, file upload, etc.. These can be easily utilized and modified.

Now you can use your Python code. In the example below I am using a saved Machine Learning algorithm I created and applying it to a file that was uploaded (I will have a separate post dedicated specifically on how to utilize a file upload template).

This file I uploaded has text data in it and a number of fields that I want to graph. Using some simple HTML and JS code, it is very simple to incorporate this into your code. The HTML will reference the chart1 and the page this will be loaded on will be found on /chart.

Any additional actions we want do perform can be done here. In our example we are creating lists of values for our graph that can be called by our HTML code.

As was mentioned earlier, the if __name__ == ‘__main__’; app.run() will wrap up all of the code within the application. In another post I will go into more detail on how to upload this .py code along with the HTML and JS into a server like Heroku where it will put your app live. But for now, hopefully this gives you a general idea of what Flask is, what it does, and the basics of how it can be used.