[tools] ngrok

Reference: https://gist.github.com/raveenb/c0001484a79214c21785227d0688f57fgist.github.com/raveenb/c0001484a79214c21785227d0688f57f 用FastAPI在colab上举例: Setup !pip install fastapi uv

Reference:

https://gist.github.com/raveenb/c0001484a79214c21785227d0688f57fgist.github.com/raveenb/c0001484a79214c21785227d0688f57f

用FastAPI在colab上举例:

Setup

!pip install fastapi uvicorn pyngrok

#也可以只安装pngrok

Make the app

from fastapi import FastAPI

app = FastAPI()

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

Ngrok channel, listening at port 8000

在ngrok上注册账号-->your tokenization --> copy token

from pyngrok import ngrok

ngrok.set_auth_token(token)
ngrok_tunnel = ngrok.connect(8000)

ngrok_tunnel.public_url

or a function:

from pyngrok import ngrok

def start_ngrok(token,port=8000):
    
    # Get token from ngrok.com --> login --> Your Authtoken
    ngrok.set_auth_token(token)
    
    # Start a tunnel on port
    ngrok_tunnel = ngrok.connect(port)
    
    # Return the public URL
    return ngrok_tunnel.public_url

把public的网址记下来

Nest the event loop and put it on port 8000

import nest_asyncio
import uvicorn

nest_asyncio.apply()
uvicorn.run(app, port=8000)

打开public网址

如果是nbdev

最后一步是

nbdev_preview --port 8000 #default is 3000