Skip to main content

Generate code for models, views, and urls based on Python type annotations.

Project description

Generate code for models, views, and urls based on Python type annotations. Powered by pydantic. Influenced by SQLModel.

reactant aims to give usable and sensible code defaults. It does not enforce a particular application structure. Instead, it follows the default/minimal/common structure of the supported frameworks, and the developer has the freedom of changing it (if necessary) to fit it to their application. Contributions are warmly welcomed if you believe a particular structure is widely used and can benefit from code generation.

Supported Frameworks

reactant currently generates code for the following:

Django REST (in Django's default project structure i.e. by apps)

  • models
  • views (class-based API views, filename=views_class.py)
  • views (function-based API views, filename=views_func.py)
  • serializers
  • urls (from class-based API views, filename=urls_class.py)
  • urls (from function-based API views, filename=urls_func.py)

Flask

  • models (Flask-SQLAlchemy)

SQLAlchemy

  • models in Declarative Mapping

Peewee

  • models

Installation

pip install reactant

NOTE: reactant isn't shipped with the supported frameworks. You MUST install django, sqlalchemy, peewee, or flask separately (be it in a virtual environment or globally).

Get Started

Create reactant models by inheriting from Reactant subclasses: DjangoORM, SQLAlchemyORM, PeeweeORM. The example below uses DjangoORM. Your choice will determine what code and files will be generated.

# generate.py

from typing import Optional
from reactant import DjangoORM, Field, generate
from datetime import date


class RocketEngine(DjangoORM):
    name: str = Field(max_length=32, title="engine_name") # adding additional arguments
    manufacturer: Optional[str]
    power_cycle: Optional[str] = "gas-generator" # setting a default
    thrust_weight_ratio: Optional[int] = None


class LaunchVehicle(DjangoORM):
    name: str = Field(max_length=32)
    country: str = Field("USA", max_length=32) # setting a default in the Field function 
    status: str
    total_launches: Optional[int]
    first_flight: Optional[date]
    engine: str = Field(foreign_key="RocketEngine") # specifying a relationship field

# Don't forget this block.
if __name__ == "__main__":
    generate()

You can give ORM-specific arguments to the Field function. For more details, please read the The Field function section.

Don't forget generate(). Run by specifying the file: reactant generate.py

Tip: You can control what code and files will be generated by passing keyword arguments to generate(). Read the The generate function section for the details.

$ reactant generate.py

Running generate.py
Found 2 Django reactants.
Django models.py finished rendering.
Django serializers.py finished rendering.
Django views_class.py finished rendering.
Django urls_class.py finished rendering.
Django views_func.py finished rendering.
Django urls_func.py finished rendering.
Success! Please check "reactant_products/django" directory.

BOOM! With just the above code, the models, views, serializers, and urls (the products, for Django atleast) are generated. See images of the code below.

Sample Code Generated

Django REST

The Field function

The reactant Field function comes from pydantic. It can be used to provide extra information about the field. Currently though, reactant Field can only support the following pydantic native arguments:

  • default (the positional argument)
  • max_length
  • title

Other than the listed native arguments, reactant Field can accept arguments that are specific to an ORM and filters out those that are not supported by the same ORM. For example if you are generating Django files, Django ORM model fields can accept an error_messages argument. You can give this argument to the reactant Field and the generated Django models will include this argument. However, if you give made-up arguments or arguments that are only valid for other ORMs, the generated Django models will not include those.

Specifying model relationships

Relationship fields can be specified by giving the reactant Field special arguments.

  • foreign_key - identifies a foreign key
  • many_key - identifies a many-to-many
  • one_key - identifies a one-to-one

The Get Started section shows an example of specifying a relationship.

The generate function

The generate function can accept keyword arguments to control the behavior of generating code and files. Read below to learn what each supported framework accepts.

Django REST

By default, both function-based and class-based views are generated. To switch OFF either of the two, pass class_based=False or function_based=False.

Development

The project uses Poetry to package and manage dependencies.

(venv)$ poetry install

Run tests.

pytest

License

MIT License. For more information and legal terms, see the LICENSE file.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

reactant-0.4.1.tar.gz (13.3 kB view details)

Uploaded Source

Built Distribution

reactant-0.4.1-py3-none-any.whl (16.5 kB view details)

Uploaded Python 3

File details

Details for the file reactant-0.4.1.tar.gz.

File metadata

  • Download URL: reactant-0.4.1.tar.gz
  • Upload date:
  • Size: 13.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.7 CPython/3.8.10 Linux/5.11.0-37-generic

File hashes

Hashes for reactant-0.4.1.tar.gz
Algorithm Hash digest
SHA256 51b1f31278caaa18742678277619d8ca09883c421e1728cc00d3169c194abc3b
MD5 d7b238528eb73c1b6810055c1ce47752
BLAKE2b-256 0d0d07532a73f4b5df2ad8d2f230d93036d2b1dd8dcba585431daf3ee83f5359

See more details on using hashes here.

File details

Details for the file reactant-0.4.1-py3-none-any.whl.

File metadata

  • Download URL: reactant-0.4.1-py3-none-any.whl
  • Upload date:
  • Size: 16.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.7 CPython/3.8.10 Linux/5.11.0-37-generic

File hashes

Hashes for reactant-0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 34536c2e65c33ce1a95bcf72f96d5d60cbbb3011e48c10686bfa10ec0d4c3891
MD5 9120955630194b689330b35c81eec99a
BLAKE2b-256 27403de8dff3c72a559d36a250c8da0c2a7039bfc825cd8538f5da8b4db71ecd

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page