Build A Web App

You can use League Manager from within a web application like Litestar, Flask, or FastAPI.

For this tutorial, I will be using Litestar, but the same concepts can be applied with other frameworks.

Installing Litestar

Important

Each time terminal commands are used, it is with the expectation that your virtual environment has been activated.

To install Litestar, use this command:

python -m pip install litestar[standard]

Adding [standard] to the litestar package ensures that a few additional dependencies are installed, including the uvicorn web server that will allow you to serve you application

App Structure

Up to this point, you don’t have much in your new-project directory, outside of the .venv virtual environment directory.

Ordinarily, you’ll want to include a few more files in your project root that might include .gitignore, pyproject.toml, and/or a requirements.txt file.

None of these are required to build your application, but they generally become extremely important for a maturing project.

For now, we will skip these optional files.

Source Directory

While you are in the new-project directory, you could create a .py file here and start building your app, but again, for a maturing project, that is not the best idea.

Instead, let’s create a directory called app where we will start building our Python modules.

Hint

A module refers to any Python file that contains definitions and statements. Or in other words, a file that ends in .py

Create an app directory:

# create the directory
mkdir app

# change into that directory
cd app

Now it’s time to start creating some files.

You could use a simple text editor to create these files, but by this point, it’s likely better to start using a code editor or IDE (integrated development environment) if you’re not using one already.

First Module

Create a file in your app directory and call it main.py.

And while you’re at it, create another file and call it __init__.py.

You’re ordinarily going to create a blank __init__.py in any directory within the app. Python uses the __init__.py file to treat that directory as a package.

Now, open the main.py file and create a minimal Litestar application.

# new-project/app/main.py

from litestar import Litestar, get


@get("/")
async def hello_world() -> str:
    return "Hello, world!"


app = Litestar([hello_world])

This is the most basic web app you could build. Though not very practical, it serves as the foundation to your application.

In order to see it working, open up your terminal and navigate once again to your new-project directory.

Type litestar run into the terminal. This command serves the application locally on your machine.

You can now visit http://127.0.0.1:8000 in your web browser and you’ll see the message written at the top of the browser screen.