Export and import for MongoEngine model in Flask-Admin

| Comments

Another tip for Flask-Admin. The task’s requirements are:

  • possibility to choose some model’s objects and download them in JSON
  • possibility to upload them back

There is a ModelView property can_export, it adds an action to export in CSV or another format supported by tablib, but it does not allow to select records and there is no import. So for my task it’s not a solution.

The export is easy to do with an action decorathor.

A bootstrap for a microservice based on Flask with MongoDB

Starting a new project is a common task in microservices architecture. To do this it’s better to have a some template. I put my version to the flask-mongoengine-bootstrap repository. The key point are:

  • very basic, only flask, flask-mongoengine and structlog in requirements
  • configuration through environment variables
  • configured logging in JSON format
  • marking log records with request_id
  • possibility to run development version make dev and tests make test through docker
  • a template for Makefile
  • examples of model, api route and tests

If you’ll use it, do not forget to change SECRET_KEY.

No-Cache headers in Flask

| Comments

The second line of “The Zen of Python” (try import this) says “Explicit is better than implicit.”. By default Flask response does not return any cache header and you can suppose that browsers will not cache, but some of them can decide for you. So it’s better to provide them, especially for API and it’s easy:

	@app.after_request
    def set_response_headers(response):
        response.headers['Cache-Control'] = 'no-cache, no-store, must-revalidate'
        response.headers['Pragma'] = 'no-cache'
        response.headers['Expires'] = '0'
        return response

Interesting articles

| Comments

Several interesting articles I found recently. Some of them I think from Dan Bader twitter.

Django vs Flask - a detailed comparison Django vs Flask.

In most cases I preferer Django.

  • It has better documentation
  • There are better apps in core, with Flask you need to search for an app, compare alternatives and sometimes they are not updated for a long time
  • You can choose which core apps to use
  • I think it’s better developed and supported

How the heck does async/await work in Python 3.5? - a long read about async in python

Using Docker for Flask Application Development (not just Production!) - i use it the same way, the only problem I have with Python development in Docker is debugging in Intellij Idea. There is a remote interpreter, but it does not work well for me, will try it again with the latest version.

Flask-RESTful migration to Flask-RESTPlus

| Comments

In a simple application with a several endpoints, it’s ok to use the default Flask routes.

@app.route('/api/v1.0/users', methods=['GET'])
def get_users():
    return jsonify({'objects': users})

But when an application becomes bigger, it can be useful to use a some REST framework or split it on the smaller apps. The REST framework is used to:

  • enforce best practices
  • simplify: versioning, serialization, documentation, authenication
  • provide browsable API

1/2 ยป