To set up Poetry you want to do the following things.
b) You want to then add an include section to your project with the destination of your start script.
In this case mine is called __init__.py and sits in the following structure:
quart_example
├── src
│ └── poetry_example
│ ├── __init__.py
│ └── ...
├── .gitignore
├── pyproject.toml
├── poetry.lock
└── ...
Thus my include statement looks like this (it goes in the [tool.poetry] section at the top level):
packages = [
{ include = "src", from = "." }
]
What does this do? It allows you to install your own project as a package into your venv. This allows you to create scripts you can run which is critical for deployment.
In this case you can run poetry add quart to install quart and then whatever else you need
Run poetry shell in order to launch your virtual environment and then run poetry install to install all dependencies including your own package. You need a README.md or the installing of your own project will fail.
With Poetry now installed, you can start developing in Quart. Let me explain the starter code (you can find it in __init__.py):
from quart import Quart, jsonify, request
app = Quart(__name__)
@app.get("/example")
async def example():
return jsonify(["a", "b"])
@app.post("/echo")
async def echo():
data = await request.get_json()
return {"input": data, "extra": True}
def run() -> None:
app.run()
- The
from quart import Quart, jsonify, requestline item is importing the outside packages you'll need. Specifically these are versions of jsonify and request bundled with quart. app = Quart(__name__)is initializing the quart application and assigning it to the variableapp@app.get("/example")is aGETendpoint. Remember there are 4 types of endpoints,GET,POST,PUT, andDELETE.GETis meant for just getting static information.- The
/examplepart of the endpoint means this is the route on the server where you can find that information. If you are running on localhost you could go tolocalhost:5000/exampleto send thatGETrequest - The async part is specific to Quart vs. Flask.
- The
@app.post("/echo")is aPOSTendpoint.POSTendpoints require data to be passed. Use aPOSTendpoint whenever you are expecting something to be sent from the user or frontend.- In this case this endpoint expects a JSON object to be passed in. If you were doing authentication you might expect a token or password from the user (as an example)
- The
def run() -> None:function is meant to be the "start the server" function.
With all of this starter code, you can now add the following to the pyproject.toml which can be used to run a script. In this case the script will call the run function and thus start the server.
Scripts are important when it comes to DevOps and being able to execute various commands to run your project automatically.
[tool.poetry.scripts]
start = "src.quart_example:run"
start is the name of the script and src.quart_example is the path to the __init__.py python script. The colon then refers to which function inside the module, in this case the run function.
By running poetry run start you are running the script called start
This repo is now deprecated