0

I created a basic application for personal use. The backed of my application uses Fast Api with a SQLite database. Usually to run my start up and run my backend server I have to use the following commands:

// Using Virtual ENV
source env/Scripts/activate

pip install -r requirements.txt
uvicorn main:app --reload

I have seen other people create a python executable before. I would like to do the same but I need it to start the uvicorn server. How do I create a python executable that runs a uvicorn server?

Or is it better to just write a batch script that does this?

ITM007
  • 67
  • 1
  • 8

1 Answers1

0

Somthing like

import uvicorn
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware

#  Import A module from my own project that has the routes defined
from redorg.routers import saved_items 

origins = [
    'http://localhost:8080',
]


webapp = FastAPI()
webapp.include_router(saved_items.router)
webapp.add_middleware(
    CORSMiddleware,
    allow_origins=origins,
    allow_credentials=True,
    allow_methods=['*'],
    allow_headers=['*'],
)


def serve():
    """Serve the web application."""
    uvicorn.run(webapp)

if __name__ == "__main__":
    serve()

If you need to pass arguments you can use something like argparse/click to expose a cli interface.

Vikash Balasubramanian
  • 2,509
  • 3
  • 29
  • 57
  • Thank you for answering so quickly... I am still a little confused, are you saying that this code will turn it into an executable? Or would I need to use a tool such as "pyinstaller"?... And for the code above i'm not understanding the ```from redorg.routers import saved_items``` What am i supposed to be importing there? For my backend I have three different files that build my sqlite database ```database.py```, ```models.py```, and ```main.py``` Am I supposed to be importing those files? – ITM007 May 09 '21 at 02:57
  • By executable do you mean an exe that can run on windows? Then yes, you need to use pyinstaller. That import is for the routes that you have defined: https://fastapi.tiangolo.com/tutorial/bigger-applications/ – Vikash Balasubramanian May 09 '21 at 03:00