Download vial locally json

I am struggling to load local json in the bulb.

from flask import Flask, render_template, json, url_for def taiwan(): json_data = open(url_for('static', filename="data/taiwan.json")) data = json.load(json_data) return render_template('taiwan.jade', data=data) 

This raises an IOError: there is no such file or directory: '/static/css/taiwan.json'. But it still exists.

Any suggestions

+8
json python flask
source share
2 answers

I ended up with this:

 import os from flask import Flask, render_template, url_for, json def showjson(): SITE_ROOT = os.path.realpath(os.path.dirname(__file__)) json_url = os.path.join(SITE_ROOT, "static/data", "taiwan.json") data = json.load(open(json_url)) return render_template('showjson.jade', data=data) 
+9
source share

You must use the path to your static directory to get the file, url_for gets the path of the abs URI, not the path of fs. try it

 json_data = open(os.path.join(your_static_dir, "data", "taiwan.json"), "r") 
+2
source share

All Articles