Skip to content

Instantly share code, notes, and snippets.

@CareTiger
Forked from raveenb/fastapi_inside_jupyter.md
Created September 18, 2023 06:32
Show Gist options
  • Save CareTiger/2f23e4b878e240b09a0a1fbfed09ccfb to your computer and use it in GitHub Desktop.
Save CareTiger/2f23e4b878e240b09a0a1fbfed09ccfb to your computer and use it in GitHub Desktop.

Revisions

  1. @raveenb raveenb revised this gist Jun 15, 2021. No changes.
  2. @raveenb raveenb revised this gist Jun 15, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion fastapi_inside_jupyter.md
    Original file line number Diff line number Diff line change
    @@ -40,4 +40,4 @@ nest_asyncio.apply()
    uvicorn.run(app, port=8000)
    ```

    - Click on the ngrok url printed above, and visit ```/docs``` and you will have the Swagger/OpenAPI page and call your api's from there
    - Click on the ngrok url printed above, and visit ```/docs``` endpoint and you will have the familiar Swagger page and call your api's from there
  3. @raveenb raveenb revised this gist Jun 15, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion fastapi_inside_jupyter.md
    Original file line number Diff line number Diff line change
    @@ -40,4 +40,4 @@ nest_asyncio.apply()
    uvicorn.run(app, port=8000)
    ```

    - Click on the ngrok url printed above, and visit /docs and you will have the Swagger/OpenAPI page and call your api's from there
    - Click on the ngrok url printed above, and visit ```/docs``` and you will have the Swagger/OpenAPI page and call your api's from there
  4. @raveenb raveenb revised this gist Jun 15, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion fastapi_inside_jupyter.md
    Original file line number Diff line number Diff line change
    @@ -40,4 +40,4 @@ nest_asyncio.apply()
    uvicorn.run(app, port=8000)
    ```

    - Click on the ngrok url, and visit /docs and you will have the Swagger/OpenAPI page and call your api's from there
    - Click on the ngrok url printed above, and visit /docs and you will have the Swagger/OpenAPI page and call your api's from there
  5. @raveenb raveenb revised this gist Jun 15, 2021. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions fastapi_inside_jupyter.md
    Original file line number Diff line number Diff line change
    @@ -24,9 +24,7 @@ def index(request: UserRequestIn):

    - Start ngrok tunnel
    ```
    import nest_asyncio
    from pyngrok import ngrok
    import uvicorn
    ngrok_tunnel = ngrok.connect(8000)
    @@ -35,6 +33,9 @@ ngrok_tunnel

    - Patch Event Loop and start server
    ```
    import nest_asyncio
    import uvicorn
    nest_asyncio.apply()
    uvicorn.run(app, port=8000)
    ```
  6. @raveenb raveenb revised this gist Jun 15, 2021. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion fastapi_inside_jupyter.md
    Original file line number Diff line number Diff line change
    @@ -1,12 +1,15 @@
    # How to Run FastAPI inside Jupyter

    - Ensure you have these installed and accessible from the notebook pyngrok, nest_asyncio, fastapi, uvicorn and other libs you want
    ```%pip install pyngrok nest_asyncio fastapi uvicorn```
    ```
    %pip install pyngrok nest_asyncio fastapi uvicorn loguru
    ```

    - Create a FastAPI app
    ```
    from fastapi import FastAPI
    from pydantic import BaseModel
    from loguru import logger
    app = FastAPI()
  7. @raveenb raveenb revised this gist Jun 15, 2021. No changes.
  8. @raveenb raveenb revised this gist Jun 15, 2021. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions fastapi_inside_jupyter.md
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    # How to Run FastAPI inside Jupyter

    - Ensure you have these installed and accessible from the notebook pyngrok, nest_asyncio, fastapi, uvicorn and other libs you want
    ```%pip install pyngrok nest_asyncio fastapi uvicorn```

  9. @raveenb raveenb revised this gist Jun 15, 2021. 1 changed file with 20 additions and 1 deletion.
    21 changes: 20 additions & 1 deletion fastapi_inside_jupyter.md
    Original file line number Diff line number Diff line change
    @@ -15,4 +15,23 @@ class UserRequestIn(BaseModel):
    def index(request: UserRequestIn):
    logger.debug(request)
    return {"ok": True}
    ```
    ```

    - Start ngrok tunnel
    ```
    import nest_asyncio
    from pyngrok import ngrok
    import uvicorn
    ngrok_tunnel = ngrok.connect(8000)
    ngrok_tunnel
    ```

    - Patch Event Loop and start server
    ```
    nest_asyncio.apply()
    uvicorn.run(app, port=8000)
    ```

    - Click on the ngrok url, and visit /docs and you will have the Swagger/OpenAPI page and call your api's from there
  10. @raveenb raveenb created this gist Jun 15, 2021.
    18 changes: 18 additions & 0 deletions fastapi_inside_jupyter.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    - Ensure you have these installed and accessible from the notebook pyngrok, nest_asyncio, fastapi, uvicorn and other libs you want
    ```%pip install pyngrok nest_asyncio fastapi uvicorn```

    - Create a FastAPI app
    ```
    from fastapi import FastAPI
    from pydantic import BaseModel
    app = FastAPI()
    class UserRequestIn(BaseModel):
    text: str
    @app.post("/test")
    def index(request: UserRequestIn):
    logger.debug(request)
    return {"ok": True}
    ```