Invalid Syntax with "await def function()" using asyncio and aiohttp

What I’m trying to do:
Asynchronous API calls

What I’ve tried and what’s not working:
Used asyncio and aioasync to do this. I can successfully run the same code in my local environment but I’m getting a Syntax Error: Bad Input issue. I’m not sure what the proper syntax for doing it in Anvil is (This is on the server side)

Code Sample:

async def getDetails():
    json = {
        "authToken": "123456-789101-11213-1415-161718192021"
        }
    results = []
    endpoints = ["/general/getdetails", "/general/getsubstances"]
    for endpoint in endpoints:
        url = "https://app.exampleapp.net/api" + endpoint
        async with aiohttp.ClientSession() as session:
            async with session.post(url, json = json) as response:
                result = await response.json()
                results.append(result["data"])

asyncio.run(getDetails())

This is probably valid in your app’s server-side code. In your app’s browser-side code, probably not.

Anvil uses Skulpt to transpile browser-side code into Javascript, so that the browser can run it. Skulpt supports some Python 3.7 features, but not all. IIRC, async and await are not supported browser-side.

1 Like

Apologies for the overlook, but I should’ve mentioned that this was in the server code.

That’s okay. Another possibility is that your server-side Python version is set to 2. IIRC, async and await were introduced in Python 3.5…

I was using the 3.10 beta but I also tried it in the full Python 3 environment to the same issue. I’m not sure if it’s because async is already reserved for some Anvil function.

Hi @lab,

Can you please share a clone link for an app that replicates this issue?

If you’re getting Syntax error: Bad Input that is almost certainly from Skulpt, which means that somewhere, some client code is trying to load this code. A clone link that replicates the problem will probably illustrate how that’s happening :slight_smile: