Advanced Program

We will write a program that retrieves aggregates from a user.

The aleph-client library is pre-installed and pre-configured in the official Aleph-VM Python runtime. It is tweaked to work even for programs with the access to internet disabled.

1 - Requirements

You need to install aleph-client for local testing.

We will use Uvicorn for local testing and FastAPI to build our program.

  • Linux/ macOs :

pip3 install "uvicorn[standard]" fastapi

2 - Writing the program

To create the first program, open your favorite code editor and create a directory named my-program, containing a file named main.py.

.
└── my-program/
    └── main.py

Then write the following code in the file:

from fastapi import FastAPI
from aleph_client.asynchronous import fetch_aggregates

app = FastAPI()

@app.get("/")
async def root():
    return {"Fetch route": "/fetch/<address>"}

@app.get("/fetch/{account}")
async def read_item(account):
    res = await fetch_aggregates(account)
    return {"item_id": res}

That's it for your first program.

This code comes from the FastAPI tutorial. Have a look at it for a better understanding of what it does and how it works.

3 - Testing locally

Before uploading your program on Aleph, it is best to first test it locally.

Aleph uses the standard ASGI interface to interface with programs written in Python. ASGI interfaces with many Python frameworks, including FastAPI but also Django or Quart.

Test your program locally using uvicorn, an ASGI server:

go to your working repository and launch:

python3 -m uvicorn main:app --reload --host=0.0.0.0

Then open http://127.0.0.1:8000 . The --reload option will automatically reload your app when the code changes.

4 - Deploy

See the publish method.

Last updated