How to load a static file in Flask app when deploying to Heroku?

Viewed 469

I have a Flask JSON API application that would read a file in the code repository and display the data in that file. My application file is as follows:

from flask import Flask, jsonify

from utils import load_data


data = load_data(os.environ.get('DATA_FILE_PATH'))  # This would be in data/data.txt for example

app = Flask(__name__)

@app.route('/', methods=['GET'])
def home():
    return jsonify(data=data)

When I deploy to Heroku, how can I set the environment variable to point to that particular data file? Locally, I would do

$ export DATA_FILE_PATH=/path/to/repo/data/data.txt

I know I can use the Heroku CLI heroku config:set DATA_FILE_PATH=<path> but I'm not sure how to place that path variable since I don't really know the directory structure on the Heroku deployment.

2 Answers

You can add them in the settings tab config vars Heroku settings tab

Okay, turns out you can go into the Heroku Dyno's bash using the following command (assuming you've already pushed to Heroku):

$ heroku run bash

and figure out what the file system structure looks like. In my particular case, my repo was served under the /app directory. So all I needed to pass into the value of the DATA_FILE_PATH environment variable is /app/data/data.txt.

Related