Skip to content

Instantly share code, notes, and snippets.

@wshayes
Last active June 28, 2023 18:24

Revisions

  1. wshayes revised this gist Apr 13, 2020. 1 changed file with 21 additions and 0 deletions.
    21 changes: 21 additions & 0 deletions minimal.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    # From: https://github.com/tiangolo/fastapi/issues/23#issuecomment-597631374
    # The following works with the pycharm debugger with reload running in a docker-compose remote interpreter

    from fastapi import FastAPI
    import uvicorn

    app = FastAPI()

    @app.get("/")
    def root():
    a = "a"
    b = "b" + a
    return {"hello world": b}


    if __name__ == '__main__':
    uvicorn.run("main:app", host='0.0.0.0', port=8000, reload=True)

    # Notice the it's "main:app" and not app otherwise you get an error:

    # WARNING: You must pass the application as an import string to enable 'reload' or 'workers'.
  2. wshayes revised this gist Mar 26, 2020. 1 changed file with 22 additions and 6 deletions.
    28 changes: 22 additions & 6 deletions fastapi_demo.py
    Original file line number Diff line number Diff line change
    @@ -1,20 +1,32 @@
    #!/usr/bin/env python3.7

    import logging

    import uvicorn
    from fastapi import FastAPI
    from starlette.responses import RedirectResponse
    from starlette.testclient import TestClient

    logger = logging.getLogger(__name__)
    logger.setLevel(logging.DEBUG)
    log = logging.getLogger(__name__)
    log.setLevel(logging.DEBUG)

    app = FastAPI()


    @app.get("/app")
    @app.get("/")
    def read_main():
    return {"message": "Hello World from main app"}


    @app.get("/test/{check:path}")
    def test_slash(check: str):
    """Test if forward slash can be captured
    https://fastapi.tiangolo.com/tutorial/path-params/#path-convertor
    """

    return {"message": check}


    subapi = FastAPI(openapi_prefix="/subapi")


    @@ -32,7 +44,7 @@ async def redirect():

    @subapi.get("/redirected")
    async def redirected():
    logger.debug("REDIRECTED")
    log.debug("REDIRECTED")
    return {"message": "you've been redirected"}


    @@ -45,4 +57,8 @@ async def redirected():
    def test_redirect_subapi():
    url = app.url_path_for("redirect")
    response = client.get(url)
    assert response.json() == {"message": "you've been redirected"}
    assert response.json() == {"message": "you've been redirected"}


    if __name__ == "__main__":
    uvicorn.run("fastapi_test:app", host="127.0.0.1", port=5000, log_level="info")
  3. wshayes revised this gist May 15, 2019. No changes.
  4. wshayes created this gist May 15, 2019.
    48 changes: 48 additions & 0 deletions fastapi_demo.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,48 @@
    import logging

    from fastapi import FastAPI
    from starlette.responses import RedirectResponse
    from starlette.testclient import TestClient

    logger = logging.getLogger(__name__)
    logger.setLevel(logging.DEBUG)

    app = FastAPI()


    @app.get("/app")
    def read_main():
    return {"message": "Hello World from main app"}


    subapi = FastAPI(openapi_prefix="/subapi")


    @subapi.get("/sub")
    async def read_sub():
    return {"message": "Hello World from sub API"}


    @subapi.get("/redirect")
    async def redirect():
    url = app.url_path_for("redirected")
    response = RedirectResponse(url=url)
    return response


    @subapi.get("/redirected")
    async def redirected():
    logger.debug("REDIRECTED")
    return {"message": "you've been redirected"}


    app.mount("/subapi", subapi)


    client = TestClient(app)


    def test_redirect_subapi():
    url = app.url_path_for("redirect")
    response = client.get(url)
    assert response.json() == {"message": "you've been redirected"}