The minimal requirement to create a Python web application using Sanic web framework is as below:

 

#index.py
from sanic import Sanic
from sanic.response import text

app = Sanic('Khmerweb')

@app.route("/")
async def home(req):
    return text("Welcome to Khmer Web Sanic!")

 

In practice, we prefer create all routes in a separated folder called “routes” in which all routes for the frontend are put in a subfolder called “routes/front" while routes for the backend are in another subfolder called “admin.”

 

#index.py
from sanic import Sanic

app = Sanic('Khmerweb')

from routes.front import index

 

#routes/front/index.py
from sanic import Sanic
from sanic.response import text

app = Sanic.get_app('Khmerweb')

@app.route("/")
async def home(req):
    return text("Welcome to Khmer Web Sanic!")

 

Once created, Sanic application can be invoked in any module using get_app() method in Sanic class. To run this minimal Sanic application, we need to open new terminal in Visual Studio Code and write on it:

 

$ sanic index.app --dev

 

If everything is OK, we can open the browser at the address http://localhost:8000/ and we will see the sentence “Welcome to Khmer Web Sanic!” as the output on the browser.

 

What is special in Sanic is that its server is ready to use for production while in many other web frameworks, their server is only for  development or for testing offline.