Converts Python API to Server
- Clone the repo
git@github.com:K-Kumar-01/Python-API-to-Server-CLI.git
. - Change the directory
cd Python-API-to-Server-CLI
- Create a virtual environment
virtualenv -p python3 venv
. - Activate the virtual environment source
venv/bin/activate
. - Run
python3 setup.py install
mylibrary --build <fileName.py>
Builds the file i.e. converts the file to a fastapi code. Currently single file with no custom imports is only available.
- Should not contain custom imports
- Should contain a class and some functions in it.
- A decorator which adds the attributes (mentioned in the sample file) on the used function. These attributes are compulsory. Apart from these any other attribute may be added but they won't be used.
A Sample structure of the file
def api(route, http_methods, *args, **kwargs):
def decorator(func):
setattr(func, 'is_api', True)
setattr(func, "_api_route", route)
setattr(func, 'http_methods', http_methods)
return func
return decorator
class MyService():
@api(route="/", http_methods=['GET'])
def predict():
return {'Hello': 'World'}
def withoutApi():
return {'I am': 'without api'}
Running the above command will generate a build
folder in the current working directory.
The build can be run locally by doing the following:
- While in virtual environment,
pip install fastapi
andpip install uvicorn[standard]
. cd build
.- Run
uvicorn main:app --reload
The server will be running on localhost:8000
mylibrary --deploy
This command deploys the build folder on vercel
.
Note: Be sure to install vercel
globally using npm i -g vercel
or yarn global add vercel
.
The command runs vercel ./build
internally.
Some questions will be asked by vercel-cli.
If successful, one can visit the url and see the project running.