Skip to main content

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'}

Metadata

Release files for fastapi-script 0.0.8

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fastapi-script 0.0.8
File Size Uploaded Provenance Actions
fastapi-script-0.0.8.tar.gz 6.8 kB Without provenance Details for fastapi-script-0.0.8.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for fastapi-script 0.0.8
File Interpreter ABI Platform Actions
fastapi_script-0.0.8-py3-none-any.whl Python 3 none any Details for fastapi_script-0.0.8-py3-none-any.whl

Total release size: 14.3 kB

Release files / fastapi-script-0.0.8.tar.gz

Download URL fastapi-script-0.0.8.tar.gz
Size 6.8 kB
Tags Source
SHA-256 checksum
How to use checksums
bb5d6b45029859426dfed277cd35490bc698769f59a9538d1d83559851efbe06
BLAKE2b-256 checksum
How to use checksums
e237c88f756432dbcb1bdb1486d1e271471e17c7c2666646f8c3ae7c85042c17
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.11

Release files / fastapi_script-0.0.8-py3-none-any.whl

Download URL fastapi_script-0.0.8-py3-none-any.whl
Size 7.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
dda0968b997897497d953f3684e2cb46c650ae1a785143db3ef7ec7de3a54129
BLAKE2b-256 checksum
How to use checksums
9e6018304e76e75d4afdab414689707e2cf065a4ec8ba4cce1fa349f56754566
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.11

Security for fastapi-script 0.0.8

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page