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_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
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")
manufacturer: Optional[str]
power_cycle: Optional[str] = "gas-generator"
thrust_weight_ratio: Optional[int] = None
class LaunchVehicle(DjangoORM):
name: str = Field(max_length=32)
country: str = Field("USA", max_length=32)
status: str
total_launches: Optional[int]
first_flight: Optional[date]
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file reactant-0.3.1.tar.gz
.
File metadata
- Download URL: reactant-0.3.1.tar.gz
- Upload date:
- Size: 10.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.7 CPython/3.8.10 Linux/5.11.0-36-generic
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | af51513ebb763c63a873bdbcaf2baabac924383356f95570d230fe09ebbdc6c7 |
|
MD5 | 0d86e1adb944b9ee710cd2a84a9fb43c |
|
BLAKE2b-256 | 6762efaf97b96d906e19cb4568c7be94abf94ee46224aca003deae81025c6e2a |
File details
Details for the file reactant-0.3.1-py3-none-any.whl
.
File metadata
- Download URL: reactant-0.3.1-py3-none-any.whl
- Upload date:
- Size: 13.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.7 CPython/3.8.10 Linux/5.11.0-36-generic
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6d623731a7017fd93200a99775c811dbdf716f9777c31820e2e67b03e1f38b38 |
|
MD5 | a00e00bceaf879f0660e769252bdfaba |
|
BLAKE2b-256 | 1e6f1ecf14414ca23473720ece60d742058990fc81d460755cef44b362baa346 |