8

How can I share the value of variables between HTTP requests in FastAPI? For instance, I have a POST request in which I get some audio files and then I convert their info into a Pandas Dataframe. I would like to send that Dataframe in a GET request, but I can't access the Dataframe on the GET request scope.

@app.post(
    path="/upload-audios/",
    status_code=status.HTTP_200_OK
)
async def upload_audios(audios: list[UploadFile] = File(...)):
    filenames = [audio.filename for audio in audios]
    audio_data = [audio.file for audio in audios]
    new_data = []
    final_data = []
    header = ["name", "file"]
    for i in range(len(audios)):
        new_data = [filenames[i], audio_data[i]]
        final_data.append(new_data)
    new_df = pd.DataFrame(final_data, columns=header)
    return f"You have uploaded {len(audios)} audios which names are: {filenames}"

@app.get("/get-dataframe/")
async def get_dataframe():
    pass
Chris
  • 18,724
  • 6
  • 46
  • 80
0x55b1E06FF
  • 538
  • 1
  • 9
  • 24
  • Store the requested data in a storage solution - like redis, sqlite, on disk, rdbms - wherever, then read it and create the dataframes when the user requests them. You'll also need to return them in a format that FastAPI can serialize properly. – MatsLindh Feb 25 '22 at 11:35
  • @MatsLindh, so I need a database. But if I dont want to use a memory mechanism, the same thing could be done with python context variables? – 0x55b1E06FF Feb 25 '22 at 19:18
  • 2
    You could store it in-memory in your process - as long as you never expect to serve more than one user, and don't plan on having multiple workers active at the same time (which would have their own memory, so the worker handling the get would not necessarily be the same as the one handling the post). Do keep the data in-process, declare a dictionary outside of the functions, then assign to a key inside the dictionary inside the function - `foo = {}` at the top, then `foo['pd'] = ..` inside your functions. – MatsLindh Feb 25 '22 at 22:57

1 Answers1

9

If you need read-only access to that variable, and/or you never expect it to be modified by some other request before reading it (in other words, you never expect to serve more than one client), as well as your app does not use several workers—since each worker has its own things, variables and memory)—you could either (as mentioned by @MatsLindh in the comments above) declare a dictionary, e.g., foo = {}, outside the endpoints (in other words, globally) and assign a key to it inside the endpoint, e.g., foo['pd'] = df, (which you can later retrieve when another request arrives), or declare your variable as global (as described here), or, preferably, store the variable on the app instance, which allows you to store arbitrary extra state using the generic app.state attribute—see this answer on how to do this before the application starts up. For example:

from fastapi import FastAPI

app = FastAPI()

@app.post("/create")
async def create_dataframe():
    # df = ...  # create the df here
    app.state.df = df

Then, inside the get_dataframe endpoint, you can retrieve the df like this:

@app.get("/get")
async def get_dataframe():
    df = app.state.df

or, if the app instance is not available in the file from which you are working (let's say you have your endpoints defined in submodules, separately from the main module, as described here), you could get the app instance from the Request object:

from fastapi import Request

@app.get('/get')
async def get_dataframe(request: Request):
    df = request.app.state.df

Otherwise, if you needed that variable/object to be shared among different clients, as well as among multiple processes/workers, that may also require read/write access to it, you should rather use a database storage, such as PostgreSQL, SQLite, MongoDB, etc., or Key-Value stores (Caches), such as Redis, Memcached, etc. You may want to have a look at this answer as well.

Chris
  • 18,724
  • 6
  • 46
  • 80