Category: Programming

2020-10-19T10:19:50Z

How to Kill a Python Thread

I'm often asked how to kill a background thread, and the answer to this question makes a lot of people unhappy: threads cannot be killed. In this article I'm going to show you two options we have in Python to terminate threads.

5 comments

2020-09-01T09:24:09Z

How to Deploy a React-Router + Flask Application

This is the third article in my "React + Flask" series, in which I discuss applications that combine a Flask API server with a React single-page application. This time I'm going to show you how to work with the popular React-Router library for React, and in particular how this library affects the production deployment of the application.

This is the third article in my "React + Flask" series. Make sure you read the first and second parts, as this part builds on the project built up to this point.

3 comments

2020-08-06T20:40:09Z

Video: How To Fix an Internal Server Error in Flask

In this beginner level video I explain what steps you need to take when you get an Internal Server Error in your Flask application.

2020-07-30T21:55:00Z

Fixing ALTER TABLE errors with Flask-Migrate and SQLite

If you've done any work with SQLite databases you surely know that this database is very limited in terms of making changes to the database schema. When working with a migration framework such as Flask-Migrate, it is common to end up with migration scripts that fail to upgrade or downgrade just because they need to remove or modify a column in a table, something that SQLite does not support.

In this article I'm going to discuss this limitation of the SQLite database, and show you a workaround that is specific to Flask-Migrate and Alembic.

2 comments

2020-07-20T10:29:00Z

Run Your Flask Regularly Scheduled Jobs with Cron

A common need of web applications is to have a periodically running task in the background. This could be a task that imports new data from third party sources, or maybe one that removes revoked tokens from your database once they have expired. In this and many other situations you are faced with the challenge of implementing a task that runs in the background at regular intervals.

This is a pattern that many people ask me about. I've seen implementations that are based on the APScheduler package, on Celery, and even homegrown solutions built inside a background thread. Sadly none of these options are very good. In this article I'm going to show you what I believe is a very robust implementation that is based on the Flask CLI and the cron service.

24 comments

2020-07-08T11:41:50Z

Handling File Uploads With Flask

A common feature in web applications is to let users upload files to the server. The HTTP protocol documents the mechanism for a client to upload a file in RFC 1867, and our favorite web framework Flask fully supports it, but there are many implementation details that fall outside of the formal specification that are unclear for many developers. Things such as where to store uploaded files, how to use them afterwards, or how to protect the server against malicious file uploads generate a lot of confusion and uncertainty.

In this article I'm going to show you how to implement a robust file upload feature for your Flask server that is compatible with the standard file upload support in your web browser as well as the cool JavaScript-based upload widgets:

Basic file upload form

70 comments

2020-06-29T17:00:56Z

Access Localhost From Your Phone Or From Anywhere In The World

Sometimes it is useful to quickly access your Flask application running on localhost from another device or location for testing purposes. In this article I'll show you how to use the pyngrok package to provision a temporary public URL for your application that works from your phone or from anywhere in the world!

9 comments

2020-06-05T10:53:34Z

Introduction to JavaScript Arrow Functions

After I published the tutorial on JavaScript Promises, several people told me that they did not know the new arrow function syntax introduced in the ES6 version of the language.

In this article I'll give you an overview of this syntax, along with information on what platforms support it and how to use them even in older platforms.

2020-05-29T22:13:32Z

Video: Using Trailing Commas in Python

In this short video I comment on the benefits of using "trailing commas" when defining data structures in Python.

2020-05-20T15:41:28Z

Introduction to JavaScript Promises

The use of callback functions is so common in JavaScript that often you find that in the callback from one asynchronous operation the only option that you have is to start another. In many cases you end up having a long chain of callbacks within callbacks, a situation that has been appropriately referenced as callback hell.

Modern JavaScript has introduced the concept of Promises to better deal with asynchronous notifications and prevent callback hell. In this article I'm going to show how to work with libraries based on promises, how to use promises in your own functions, and how to upgrade callback-based functions to promises.

4 comments