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.

Categories
Python Answers

How to generate Python Django models from the database?

Sometimes, we want to generate Python Django models from the database.

In this article, we’ll look at how to generate Python Django models from the database.

How to generate Python Django models from the database?

To generate Python Django models from the database, we run manage.py with the inspectdb option.

For instance, we run

python manage.py inspectdb > models.py

to make Django inspect the database the app is connected to, generate the models, and save the model class code to models.py.

Conclusion

To generate Python Django models from the database, we run manage.py with the inspectdb option.

Categories
Python Answers

How to add a multiple choice field with Python Django?

Sometimes, we want to add a multiple choice field with Python Django.

In this article, we’ll look at how to add a multiple choice field with Python Django.

How to add a multiple choice field with Python Django?

To add a multiple choice field with Python Django, we can add a ManyToManyField.

For instance, we write

class Choices(models.Model):
  description = models.CharField(max_length=300)

class Profile(models.Model):
  user = models.ForeignKey(User, blank=True, unique=True, verbose_name='user')
  choices = models.ManyToManyField(Choices)

to create the choices field in the Profile model which is a ManyToManyField.

We use Choices as the argument to allow Choices values as the values for choices.

Conclusion

To add a multiple choice field with Python Django, we can add a ManyToManyField.