12

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
md598
  • 123
  • 1
  • 6
  • Does this answer your question? [How to run FastAPI application inside Jupyter?](https://stackoverflow.com/questions/74070505/how-to-run-fastapi-application-inside-jupyter) – Chris Oct 23 '22 at 08:14

2 Answers2

28

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)
alexdlaird
  • 1,174
  • 12
  • 34
Yagiz Degirmenci
  • 16,595
  • 7
  • 65
  • 85
  • 2
    Wow, very impressive. Are there any limitations to be aware of when running it like this? Perhaps I should run the server from one notebook and point to a .py file in my gdrive I'm updating as the server main? My use case is ML inference/prediction APIs I've got working locally on my laptop. Any insight would be appreciated! Thanks again. – md598 Sep 10 '20 at 18:06
2

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

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 as well.


EDIT: The above works fine in local Jupyter, but since Colab still doesn't support top-level await statements (as of July 2022) you'd need to replace the last line of the snippet above with something like:

import asyncio
loop = asyncio.get_event_loop()
loop.create_task(server.serve())
Gustavo Bezerra
  • 9,984
  • 4
  • 40
  • 48
  • Thanks for this, but what is the url when you run this? The accepted answer has a print statement - can you please share one for this method? – nickc Sep 15 '22 at 17:12
  • Uvicorn defaults to serving on 127.0.0.1:8000. See https://github.com/encode/uvicorn/blob/master/uvicorn/config.py#L212. – Gustavo Bezerra Sep 28 '22 at 08:04