https://github.com/k-kumar-01/python-api-to-server-cli
Converts Python API to Server
https://github.com/k-kumar-01/python-api-to-server-cli
argparse fastapi python uvicorn
Last synced: 2 months ago
JSON representation
Converts Python API to Server
- Host: GitHub
- URL: https://github.com/k-kumar-01/python-api-to-server-cli
- Owner: K-Kumar-01
- License: apache-2.0
- Created: 2021-07-28T22:26:01.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2021-07-29T00:11:33.000Z (almost 5 years ago)
- Last Synced: 2025-03-31T12:09:57.374Z (about 1 year ago)
- Topics: argparse, fastapi, python, uvicorn
- Language: Python
- Homepage: https://my-library-zeta.vercel.app/
- Size: 13.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Converts Python API to Server
## How to Use
1. Clone the repo `git@github.com:K-Kumar-01/Python-API-to-Server-CLI.git`.
2. Change the directory `cd Python-API-to-Server-CLI`
3. Create a virtual environment `virtualenv -p python3 venv`.
4. Activate the virtual environment source `venv/bin/activate`.
5. Run `python3 setup.py install`
## Commands
`mylibrary --build `
Builds the file i.e. converts the file to a fastapi code.
Currently single file with no custom imports is only available.
## Structure of file
1. Should not contain custom imports
2. Should contain a class and some functions in it.
3. 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
```python
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:
1. While in virtual environment, `pip install fastapi` and `pip install uvicorn[standard]`.
2. `cd build`.
3. 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.