Python Web Development: Time to REST Again
Python Web Development: Time to REST Again
In this post I will show you how to start with Python web-development using Flask. We will build a simple REST API and connect it to a front-end.
Join the DZone community and get the full member experience.Join For Free
There’s a bunch of things that I love that I don’t talk about: The IQueryable vs IEnumerable, Code-First vs Database-First, ORM vs Raw SQL or who is the most powerful Avenger in MCU. But one of the things that I’m not at all ashamed about is my appreciation for REST.
Don’t worry, we are not going to discuss what makes a true REST design and what are the benefits of pragmatic REST approaches. REST is also not an answer to every challenge. The reason I like it is due to its simplicity and how easy it is to use across various programming languages.
In this post, I will develop a simple web application using Python in which I use REST to communicate with the server from the UI. You will also see the steps and components involved, and it should give you enough context to move forward using Python in your development tasks.
Before we start the coding part, I would like to show you the application we are building today. You can access the finished application on this URL and find below the screenshot of the main page.
It is a single page application, which shows random quotes (updates after 8-second intervals or on click of the button). The source code for this application is available at this git-hub link.
Python is an interpreted, high-level, general-purpose programming language. It is very popular and is being used in many different areas of software development. However in this post, the focus is on web-development and particularly Flask. I am assuming you have some basic knowledge of Python and you are already familiar with the concept of package-management pip and virtual environment setup. I will not go into those in detail, but if you don’t know how to do this, then there are many good resources available online, which will walk you through those steps.
If you want to build a web application using python, Flask can be used. Find below some information about Flask and why you would like to use it.
- Flask is a micro-framework for building web applications using Python.
- Why choose flask?
- It's ideal for small apps because it's clean and simple.
- In many cases, you’ll be able to build your whole application in a single Python file.
- It's unopinionated, so it's flexible, especially compared to Django, which is big and opinionated.
- It's well documented, popular, and has a large community.
- Features provided by Flask
- Flask is built on two main components
- Jinja 2 (template engine for HTML).
- Werkzeug (german word: tool) provides HTTP support and routing URLs to Python functions.
- It also includes built-in dev-server, debugger, and unit-testing support.
You can install it using pip as follows:
Here is the application code which is very simple. We define a simple route which will return a string in this case.
Running a Flask App
I am using Windows. If you using linux/mac-os, then setting environment variable syntax is slightly different. Generally, there are three steps involved:
- Tell flask in which module our application can be found by setting a variable $Env:FLASK_APP=”basic-demo.py”.
- $Env:FLASK_ENV=”development” (this will enable debugger as well).
- Flask run + browse to url or you can also use >>python -m flask run
Flask comes with templates, and they are just like templates in any other programming language. See how to create them and use them below.
Create a templates folder and create an HTML file as follows (this the location where you put your HTML templates. Also notice that there is a static folder as well, which is where you put static resources.
The following template looks like standard HTML and the place-holder for dynamic data.
A Data Layer
For this demo, I am using in-memory data (quotes.json), but this could be coming from a database. To access this data, I created repo.py that contains the functions for that purpose.
Then, use this repo for your REST API. The following screenshots shows these components:
The front-end code is very boiler plate. I actually reused most of it from my earlier posts. Feel free to check it on my GitHub repo as well.
For deployment, you have multiple options. For this demo, I used an Azure Web App, which was very simple to use. Azure has great documentation about how to do that, so I won't go into details about it.
Flask makes it easy to create web applications using Python. Django is an other option, but it's a big framework, and if your requirements are simple, then Flask is easy to learn and fun to use. I enjoyed creating web applications with it and hopefully you will too. I will write more about Python soon. Until next time, happy coding.
You can check the application on this URL.
Published at DZone with permission of Jawad Hasan Shani . See the original article here.
Opinions expressed by DZone contributors are their own.