Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
470 views
in Technique[技术] by (71.8m points)

javascript - 如何将烧瓶返回值作为Java脚本函数的变量传递(How to pass flask return value as a variable for java script function)

I have written an app in flask which return list of values to html page and that values I passed to a variable in HTML to call java script function.

(我在flask中编写了一个应用程序,该应用程序将值列表返回到html页面,并且将值传递给HTML中的变量以调用Java脚本函数。)

I tried to execute below code but it showing an error message:

(我尝试执行以下代码,但显示错误消息:)

jinja2.exceptions.UndefinedError
jinja2.exceptions.UndefinedError: 'jsonfile' is undefined 

File "C:NGConf-Explorermyprojectemplatesindex.html", line 27, in top-level template code
JSONTREEVIEWER.processJSONTree('{{ url_for('static', filename=[jsonfile[0]])}}','0');
File "C:NGConf-Explorermyprojectvenvlibsite-packagesjinja2environment.py", line 411, in getitem
return obj[argument]
jinja2.exceptions.UndefinedError: 'jsonfile' is undefined

FLASK code:

(FLASK代码:)

@app.route('/')
def home():
    if not session.get('logged_in'):
        return render_template('login.html')
    else:
        # Open a file
        #path= r"C:NGConf-Explorermyprojectyang\"
        #dirs = os.listdir(path)
        filename = []
        dirs = ['Pyang_output.json2', 'Pyang_output.json3']
        for file in dirs:
            #filename.append(file).as_uri()
            #print(filename)
            filename.append(file)
        return render_template('index.html',filename=filename)

HTML code:

(HTML代码:)

<script type="text/javascript">
$(function(){
    JSONTREEVIEWER.init();
    file = {{filename|tojson}}; 
    var i;
     for (i = 0; i < file.length; i++) { 
             JSONTREEVIEWER.processJSONTree('{{ url_for('static', 
              filename=file[0])}}','0');
      } 
  ask by hemant belwal translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I suspect that you're confusing template expansion time with JavaScript execution time.

(我怀疑您将模板扩展时间与JavaScript执行时间混淆了。)

In

(在)

$(function(){
    JSONTREEVIEWER.init();
    jsonfile = {{filename|tojson}};
    console.log(jsonfile[0])
    JSONTREEVIEWER.processJSONTree('{{ url_for('static', filename=jsonfile[0])}}','0');

The template is provided with filename (which is a list of names), but not with jsonfile , which is a name that JavaScript will see once the expanded template is delivered to the browser and JavaScript executes this fragment.

(模板提供有filename (这是名称的列表),但没有提供jsonfile ,这是在将扩展模板交付给浏览器并且JavaScript执行此片段时JavaScript将看到的名称。)

But before that can happen, Jinja2 attempts to expand url_for() , which requires that it be provided with a jsonfile binding.

(但是在此之前,Jinja2尝试扩展url_for() ,这需要为其提供jsonfile绑定。)

Perhaps you meant to do something like

(也许你打算做类似的事情)

JSONTREEVIEWER.processJSONTree('{{ url_for('static', filename=filename[0])}}','0');

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...