Question: I am trying to run a "local" web app on Google Colab using FastAPI / Uvicorn like some of the Flask app sample code I've seen but cannot get it to work. Has anyone been able to do this? Appreciate it.

### Installed FastAPI & Uvicorn successfully

```
!pip install FastAPI -q
!pip install uvicorn -q

```

### Sample app

```
from fastapi import FastAPI

app = FastAPI()

@app.get("/")
async def root():
    return {"message": "Hello World"}

```

### Run attempts

```
#attempt 1
if __name__ == "__main__":
    uvicorn.run("/content/fastapi_002:app", host="127.0.0.1", port=5000, log_level="info")

```

### 

```
#attempt 2
#uvicorn main:app --reload
!uvicorn "/content/fastapi_001.ipynb:app" --reload

```

Answer: 
You can use **ngrok** to export a port as an external url. Basically, ngrok takes something available/hosted on your localhost and exposes it to the internet with a temporary public URL.

First install the dependencies

```
!pip install fastapi nest-asyncio pyngrok uvicorn

```

Create your app

```
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware

app = FastAPI()

app.add_middleware(
    CORSMiddleware,
    allow_origins=['*'],
    allow_credentials=True,
    allow_methods=['*'],
    allow_headers=['*'],
)

@app.get('/')
async def root():
    return {'hello': 'world'}

```

Then run it down.

```
import nest_asyncio
from pyngrok import ngrok
import uvicorn

ngrok_tunnel = ngrok.connect(8000)
print('Public URL:', ngrok_tunnel.public_url)
nest_asyncio.apply()
uvicorn.run(app, port=8000)

```
A simpler approach without having to use ngrok or nest-asyncio:

```py
from fastapi import FastAPI
from uvicorn import Config, Server

app = FastAPI()

@app.get("/")
async def root():
    return {"message": "Hello World"}

config = Config(app)
server = Server(config=config)
await server.serve()

```

This won't do any multi-processing or hot-reloading but gets the job done if you just want to quickly run the a simple ASGI app from Jupyter.

This can be achieved using [Hypercorn](https://pgjones.gitlab.io/hypercorn/how_to_guides/api_usage.html) as well.

---

**EDIT**: The above works fine in local Jupyter, but since Colab [still doesn't support top-level `await` statements](https://github.com/googlecolab/colabtools/issues/1553) (as of July 2022) you'd need to replace the last line of the snippet above with something like:

```py
import asyncio
loop = asyncio.get_event_loop()
loop.create_task(server.serve())

```