Categories
Python Answers

How to serve multiple clients using just Python Flask app.run() as standalone?

Sometimes, we want to serve multiple clients using just Python Flask app.run() as standalone.

In this article, we’ll look at how to serve multiple clients using just Python Flask app.run() as standalone.

How to serve multiple clients using just Python Flask app.run() as standalone?

To serve multiple clients using just Python Flask app.run() as standalone, to set the threaded argument to False.

For instance, we write

if __name__ == '__main__':
    app.run(threaded=False, processes=3)

to call app.run with threaded set to False and `proceses set to 3 to spawn 3 processes.

Conclusion

To serve multiple clients using just Python Flask app.run() as standalone, to set the threaded argument to False.

Categories
Python Answers

How to get the different parts of a Python Flask request’s URL?

Sometimes, we want to get the different parts of a Python Flask request’s URL.

In this article, we’ll look at how to get the different parts of a Python Flask request’s URL.

How to get the different parts of a Python Flask request’s URL?

To get the different parts of a Python Flask request’s URL, we can use various properties of request.

For instance, if we have

http://www.example.com/myapplication

as the root URL and we make a request to

http://www.example.com/myapplication/foo/page.html?x=y

Then we can get all parts of http://www.example.com/myapplication/foo/page.html?x=y by using the following properties

  • path – /foo/page.html
  • full_path – /foo/page.html?x=y
  • script_root – myapplication
  • base_url – http://www.example.com/myapplication/foo/page.html
  • url – http://www.example.com/myapplication/foo/page.html?x=y
  • url_root – http://www.example.com/myapplication/

Conclusion

To get the different parts of a Python Flask request’s URL, we can use various properties of request.

Categories
Python Answers

How to test code that requires a Python Flask app or request context?

Sometimes, we want to test code that requires a Python Flask app or request context.

In this article, we’ll look at how to test code that requires a Python Flask app or request context.

How to test code that requires a Python Flask app or request context?

To test code that requires a Python Flask app or request context, we can call test_client to return a test version of the app.

For instance, we write

c = app.test_client()
response = c.get('/test/url')

to call test_client on the Flask app.

And then we call get to make a GET request.

We get the app context with

with app.app_context():
    # test code

And we get the request context with:

with current_app.test_request_context():
    # test code

Conclusion

To test code that requires a Python Flask app or request context, we can call test_client to return a test version of the app.

Categories
Python Answers

How to jsonify a SQLAlchemy result set in Python Flask?

Sometimes, we want to jsonify a SQLAlchemy result set in Python Flask.

In this article, we’ll look at how to jsonify a SQLAlchemy result set in Python Flask.

How to jsonify a SQLAlchemy result set in Python Flask?

To jsonify a SQLAlchemy result set in Python Flask, we create our own serialize class.

For instance, we write

from sqlalchemy.inspection import inspect

class Serializer(object):

    def serialize(self):
        return {c: getattr(self, c) for c in inspect(self).attrs.keys()}

    @staticmethod
    def serialize_list(l):
        return [m.serialize() for m in l]

to create a Serializer class that has the static serialize_list method.

Then we use it in our model class by writing

class User(db.Model, Serializer):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String)
    password = db.Column(db.String)

    # ...

    def serialize(self):
        d = Serializer.serialize(self)
        del d['password']
        return d

We call Serializer.serialize with the self User model instance to return a dict that we can return as JSON.

Conclusion

To jsonify a SQLAlchemy result set in Python Flask, we create our own serialize class.

Categories
Docker

How to fix npm install not installing packages with docker-compose?

Sometimes, we want to fix npm install not installing packages with docker-compose.

In this article, we’ll look at how to fix npm install not installing packages with docker-compose.

How to fix npm install not installing packages with docker-compose?

To fix npm install not installing packages with docker-compose, we should remove the volume setting in our docker-compose.yml file.

For instance, we write

version: "3"
services:
  frontend:
    build: ./frontend
    ports:
      - "8091:8091"
  backend:
    build:
      context: ./backend
      dockerfile: ./Dockerfile.prod
    ports:
      - "8090:8090"

in the docker-compose.yml which has no reference to the volume setting.

This will ensure that the node_modules folder won’t be deleted after npm install is run.

Conclusion

To fix npm install not installing packages with docker-compose, we should remove the volume setting in our docker-compose.yml file.