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 it is up to the developer to make use of the generated code 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_function.py)
  • serializers
  • urls (from class-based API views, filename=urls_class.py)
  • urls (from function-based API views, filename=urls_function.py)

Flask

  • models (Flask-SQLAlchemy)

SQLAlchemy

  • models in Declarative Mapping

Peewee

  • models

Installation

$ pip install reactant

Get Started

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

# generate.py

from typing import Optional
from reactant import Reactant, DjangoORM, Field, generate


class RocketEngine(Reactant, DjangoORM):
    name: str = Field(max_length=32, title="engine_name")
    manufacturer: str = Field(max_length=64)
    power_cycle: Optional[str] = Field("gas-generator", blank=True, max_length=32)
    thrust_weight_ratio: Optional[int] = None


class LaunchVehicle(Reactant, DjangoORM):
    name: str = Field(max_length=32)
    country: str = Field(blank=True, max_length=32)
    status: str
    total_launches: Optional[int]
    engine: str = Field(foreign_key="RocketEngine")

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

Don't forget generate(). Run the code.

$ reactant generate.py

Running generate.py
Found 2 Django reactants.
Django models.py finished rendering.
Django views_class.py finished rendering.
Django serializers.py finished rendering.
Django urls_class.py finished rendering.
Success! Please check "reactant_products" 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

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.2.1.tar.gz (9.7 kB view details)

Uploaded Source

Built Distribution

reactant-0.2.1-py3-none-any.whl (11.4 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for reactant-0.2.1.tar.gz
Algorithm Hash digest
SHA256 2c68e6d7702fa55be3cc6b08fdcd3d588ba1eb3791ecc1be0e6f96e218914abe
MD5 c288379a42781cc3c13c33330521ccdd
BLAKE2b-256 d368c974ee419b3da3b6884bcef53eff69e558573c163c8f59c39a65d5d69dd0

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for reactant-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 cfec50524d2c4f22932902ae01c15aac732a538231b958d44b7b8ae0e9bb9d73
MD5 9b832d150265847d1a0b1fd27fe7e86a
BLAKE2b-256 213a3b95e0e011b142f28261a1fd6410e0837be25a08030ba6f5eed3059eea96

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