I searched for this error, but couldn't find out how to handle it. I am getting the following error, when trying to open a file:
[Errno 36] File name too long: '/var/www/FlaskApp/FlaskApp/templates/
Here is my simple code. I am trying to open a json file and render it with Flask into a website:
@app.route("/showjson/")
def showjson():
SITE_ROOT = os.path.realpath(os.path.dirname(__file__))
data_in = open(os.path.join(SITE_ROOT, "static/data", "btc.json"), "r")
data_out = ""
for line in data_in:
data_out += line.rstrip()
data_in.close()
return render_template(data_out)
Does anybody know a solution? Many thanks in advance.
You are passing the render_template function the entirety of your JSON file, when it is looking for the filename of a template file. This is why you are getting a file name is too long error.
You can use the send_from_directory function to send the JSON file. Import the function first:
from flask import send_from_directory
Then use it like so:
@app.route("/showjson/")
def showjson(path):
SITE_ROOT = os.path.realpath(os.path.dirname(__file__))
return send_from_directory(os.path.join(SITE_ROOT, "static/data"), "btc.json")