Skip to main content

An extension for FastAPI

Project description

FastAPI-Script

An extension for FastAPI.

Getting Started

Installation

First you have to install fastapi-script like this:

pip install fastapi-script

Quick Tutorial

We need to create a Python module to manage your script commands, such as manage.py:

import asyncio

from fastapi import FastAPI
from fastapi_script import Manager


app = FastAPI()
manager = Manager(app)

@manager.command
async def test_without_params():
    print('test_without_params')

@manager.command
async def test_one_params(name):
    print(f'test_one_params, name: {name}')

@manager.command
async def test_many_params(name, age=0):
    print(f'test_many_params, name: {name}, age: {age}')

@manager.option('-n', '--name', dest='name', help='input your name')
async def option_one_params(name):
    print(f'option_one_params, name: {name}')

@manager.option('-n', '--name', dest='name', help='input your name')
@manager.option('-s', '--score', dest='score', help='input your score')
async def option_many_params(name, score):
    print(f'option_many_params, name: {name}, score: {score}')


if __name__ == '__main__':
    asyncio.run(manager.run())

Next, we can manipulate the above script like this:

$ python manage.py test_without_params
test_with_no_param

$ python manage.py test_one_params laozhang
test_one_params, name: laozhang

$ python manage.py test_many_params laozhang --age 20
test_many_params, name: laozhang, age: 20

$ python manage.py option_one_params -n laozhang
option_one_params, name: laozhang

$ python manage.py option_one_params --name laozhang
option_one_params, name: laozhang

$ python manage.py option_many_params -n laozhang -s 90
option_many_params, name: laozhang, score: 90

$ python manage.py option_many_params --name laozhang --score 90
option_many_params, name: laozhang, score: 90

We can use the --help parameter to view all of the currently managed commands:

python manage.py --help

In addition to using the decorator above, we can also use the inherited Command class to do this:

import asyncio

from fastapi import FastAPI
from fastapi_script import Manager, Command, Option

app = FastAPI()
manager = Manager(app)


class HelloCommand(Command):

    def get_options(self):
        return [
            Option('-n', '--name', dest='name', help='input your name'),
            Option('-s', '--score', dest='score', help='input your score')
        ]

    def run(self, *args, **kwargs):
        print(f'hello command, args: {args}, kwargs: {kwargs}')


manager.add_command('hello', HelloCommand())

if __name__ == '__main__':
    asyncio.run(manager.run())

And then we can use it like this:

$ python manage.py hello -n laozhang -s 90
hello command, args: (), kwargs: {'name': 'laozhang', 'score': '90'}

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

fastapi-script-0.0.1.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

fastapi_script-0.0.1-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file fastapi-script-0.0.1.tar.gz.

File metadata

  • Download URL: fastapi-script-0.0.1.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.11

File hashes

Hashes for fastapi-script-0.0.1.tar.gz
Algorithm Hash digest
SHA256 d5b3472c59be5125c91df6b0dfaf105854734f3b5a7c87f593ebbc1d956f6f7b
MD5 e7e054a067e3674b041e67269ef9be28
BLAKE2b-256 4b5bc5a977d5e7e046a656df3af9ce560fd906aa0c5205bc6546e2cd55f63992

See more details on using hashes here.

File details

Details for the file fastapi_script-0.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_script-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2baa8f88db7d95fc600b707efeabd9ac1e100c6e28ce0b34af97179ed8d00f8f
MD5 78445d2f7297e2c11e93b31bcbff7ffe
BLAKE2b-256 a0326b8415abe620aa978225e9a73c8b09880e33e1c408ff0cc6f5e2ded3e2db

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