All Projects → neil-vqa → reactant

neil-vqa / reactant

Licence: MIT License
Generate code for "models, views, and urls" based on Python type annotations. Supports Django REST, SQLAlchemy, Peewee.

Programming Languages

python
139335 projects - #7 most used programming language
Jinja
831 projects
shell
77523 projects

Projects that are alternatives of or similar to reactant

djantic
Pydantic model support for Django
Stars: ✭ 256 (+1728.57%)
Mutual labels:  django-orm, pydantic
flask-tweeeter
A full-stack Twitter clone made using the Flask framework for Python 🐦
Stars: ✭ 28 (+100%)
Mutual labels:  sqlalchemy, jinja2
Statik
Multi-purpose static web site generator aimed at developers.
Stars: ✭ 249 (+1678.57%)
Mutual labels:  sqlalchemy, jinja2
chm-documentation
chm documentation PostgreSQL pgadmin3 SQLAlchemy Django Flask jinja2 webpy doc chm compiled html help Postgres Postgre документация russian
Stars: ✭ 17 (+21.43%)
Mutual labels:  sqlalchemy, jinja2
pypackage
Cookiecutter python package using Poetry, mypy, black, isort, autoflake, pytest, mkdocs, and GitHub Actions
Stars: ✭ 12 (-14.29%)
Mutual labels:  black, pydantic
pyramid-cookiecutter-alchemy
[DEPRECATED - Please use https://github.com/pylons/pyramid-cookiecutter-starter instead] A Cookiecutter (project template) for creating a Pyramid project using SQLite for persistent storage, SQLAlchemy for an ORM, Alembic for database migrations, URL dispatch for routing, and Jinja2 for templating.
Stars: ✭ 39 (+178.57%)
Mutual labels:  sqlalchemy, jinja2
django-sqlalchemy
Django ORM built on top of SQLalchemy core 2.0 for seamless integration of SQLAlchemy with Django 4.1+ PostgreSQL 14+ only for now. [pre POC now]
Stars: ✭ 101 (+621.43%)
Mutual labels:  sqlalchemy, django-orm
pydbantic
A single model for shaping, creating, accessing, storing data within a Database
Stars: ✭ 137 (+878.57%)
Mutual labels:  sqlalchemy, pydantic
flaskbooks
A very light social network & RESTful API for sharing books using flask!
Stars: ✭ 19 (+35.71%)
Mutual labels:  sqlalchemy, jinja2
goodconf
Transparently load variables from environment or JSON/YAML file.
Stars: ✭ 80 (+471.43%)
Mutual labels:  pydantic
vscode-django
Beautiful syntax and snippets for perfectionists with deadlines
Stars: ✭ 113 (+707.14%)
Mutual labels:  jinja2
fastrates
💵 Free & open source API service for current and historical foreign exchange rates.
Stars: ✭ 26 (+85.71%)
Mutual labels:  sqlalchemy
rubric
Linter Config Initializer for Python
Stars: ✭ 21 (+50%)
Mutual labels:  black
bottle-jinja-live
A small validator for jinja2 template
Stars: ✭ 14 (+0%)
Mutual labels:  jinja2
jinja2-action
Use the Jinja2 template engine as a GitHub action
Stars: ✭ 27 (+92.86%)
Mutual labels:  jinja2
ODetaM
A simple ODM(Object Document Mapper) for Deta Base based on pydantic
Stars: ✭ 18 (+28.57%)
Mutual labels:  pydantic
fastapi-lazy
Lazy package to start your project using FastAPI✨
Stars: ✭ 84 (+500%)
Mutual labels:  pydantic
coAST
Universal and language-independent abstract syntax tree
Stars: ✭ 30 (+114.29%)
Mutual labels:  jinja2
telethon-session-sqlalchemy
SQLAlchemy backend for Telethon session storage
Stars: ✭ 34 (+142.86%)
Mutual labels:  sqlalchemy
server
The ViUR application development framework - legacy version 2.x for Python 2.7
Stars: ✭ 12 (-14.29%)
Mutual labels:  jinja2

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 (Django 3+, DRF 3.12+)

(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)
  • views (modelviewset, filename=views_modelviewset.py)
  • serializers
  • urls (using class-based API views, filename=urls_class.py)
  • urls (using function-based API views, filename=urls_func.py)
  • urls (using viewset, filename=urls_viewset.py)

Flask (Flask 2+)

(package structure, for building REST APIs)

  • views

SQLAlchemy (SQLAlchemy 1.4+)

  • classes mapped by Declarative Mapping
  • classes mapped by Classical Mapping

Peewee (Peewee 3.14+)

  • 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

dj_shot_01 dj_shot_02 dj_shot_03 dj_shot_04

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

These behave differently for each framework. The Get Started section shows an example of specifying a relationship.

Django REST: Models inheriting from DjangoORM can use foreign_key, many_key, and one_key to establish relationships. The Get Started section shows a DjangoORM example.

SQLAlchemy: reactant aims not to introduce anything new except having knowledge of type hints and pydantic. It was hard to capture and generate SQLAlchemy relationships without introducing new reactant concepts due to SQLAlchemy's flexibility. Nevertheless, relationship code are still generated and you are given options as to what relationship you intend to build.

Currently, only the foreign_key parameter is available for establishing relationships. You may put the Field function with this parameter to any of your classes since the generated code will provide options for your intended relationship: One-to-Many, Many-to-One, One-to-One. Using SQLAlchemyORM for the reactant models in Get Started section, the following code is generated:

sqla_rel_01

Peewee: reactant models with PeeweeORM can use foreign_key parameter.

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, all of function-based, class-based, and viewset-based views are generated. To switch OFF any of them, pass class_based=False or function_based=False or viewset_based=False

Development

The project uses Poetry to package and manage dependencies.

(venv)$ poetry install

Run tests.

pytest

Do linting.

bash scripts/lint-check.sh

Fix formatting.

bash scripts/format.sh

License

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].