Category: Flask

2019-03-18T17:32:48Z

Running a Flask Application as a Service with Systemd

Posted by Miguel Grinberg under Python, Flask.

When you deploy your application on a server, you need to make sure the application runs uninterrupted. If the application crashes, you'd want it to automatically restart, and if the server experiences a power outage, you'd want the application to start immediately once power is restored. Basically what you need is something that keeps an eye on the application and restarts it if it ever finds that it isn't running anymore.

In previous tutorials, I showed you how to implement this using supervisord, which is a third party utility written in Python. Today I'm going to show you a similar solution based on systemd, which is a native component in many Linux distributions including Debian derivatives such as Ubuntu and RedHat derivatives such as Fedora and CentOS.

8 comments

2019-02-16T19:29:15Z

Unit Testing Applications that use Flask-Login and Flask-SocketIO

One of the useful features of my Flask-SocketIO extension is the test client, which allows you to write Socket.IO unit tests. A long time limitation of the test client was that it did not see cookies set by Flask, such as the user session. This complicated writing Socket.IO tests for applications that require authentication, because most authentication mechanisms write something to the user session or a custom cookie. The use case that caused pain to a lot of developers was applications that use Flask-Login combined with Flask-SocketIO. To unit test such an application you had to resort to weird tricks such as mocking the current_user variable.

I recently came up with a solution to this problem, so I'm glad to report that this limitation is now a thing of the past. In this short article I want to show you how to set up your project to take advantage of the new cookie support in the Socket.IO test client.

9 comments

2018-07-23T17:01:26Z

Setting Up a Flask Application in PyCharm

In this short article and video I want to give you a few tips on setting up a PyCharm project for your Flask application. The idea is to set up a Flask application so that it can be executed, debugged, and tested from inside PyCharm Community Edition, which is fantastic IDE for Python that is completely free to download and use. If you want to see me go through the exercise, watch the video below. Then you can come to the article if you want a quick reference and summary of the steps required.

20 comments

2018-05-27T17:11:36Z

Flask Webcast #3: Circular Dependencies

In this live webcast I explain how to prevent circular dependency errors in Python. Most of this presentation is a live coding session in which I refactor a single-file Flask application into a fully fleshed out structure with multiple packages and modules, demonstrating how I completely avoid circular dependencies.

2 comments

2018-05-08T20:47:45Z

The Flask Mega-Tutorial Part XXIII: Application Programming Interfaces (APIs)

This is the twenty third and last installment of the Flask Mega-Tutorial series, in which I'm going to tell you how to extend microblog with an application programming interface (or API) that clients can use to work with the application in a more direct way than the traditional web browser workflow.

92 comments

2018-05-01T23:17:57Z

The Flask Mega-Tutorial Part XXII: Background Jobs

This is the twenty second installment of the Flask Mega-Tutorial series, in which I'm going to tell you how to create background jobs that run independently of the web server.

93 comments

2018-04-24T20:43:57Z

The Flask Mega-Tutorial Part XXI: User Notifications

This is the twenty first installment of the Flask Mega-Tutorial series, in which I'm going to add a private message feature, along with user notifications that appear in the navigation bar without the need to refresh the page.

34 comments

2018-04-22T04:08:06Z

Flask Webcast #2: Request and Application Contexts

In this live webcast I explain what is the purpose of the request and application contexts in Flask, and why sometimes you get these weird errors about them.

2 comments

2018-04-17T19:20:08Z

The Flask Mega-Tutorial Part XX: Some JavaScript Magic

This is the twentieth installment of the Flask Mega-Tutorial series, in which I'm going to add a nice popup when you hover your mouse over a user's nickname.

29 comments

2018-04-10T17:58:26Z

The Flask Mega-Tutorial Part XIX: Deployment on Docker Containers

This is the nineteenth installment of the Flask Mega-Tutorial series, in which I'm going to deploy Microblog to the Docker container platform.

84 comments