简体   繁体   English

为什么只有在Flask中执行JavaScript时,JavaScript才应访问Flask发送的列表<script> tag in HTML?

[英]Why should JavaScript access to a list sent by Flask only if it is executed within the <script> tag in HTML?

I would like to send a list to JavaScript from Flask. 我想从Flask发送一个列表到JavaScript。 I noticed that if I write JavaScript code inside an HTML tag it works, while if I do the same thing in a file linked inside the header I get the following error: 我注意到,如果我在HTML标记内编写JavaScript代码,那么它会起作用,而如果我在标头内链接的文件中执行相同的操作,则会收到以下错误:

Uncaught SyntaxError: Unexpected token { 未捕获到的SyntaxError:意外令牌{

The javascript code is as follows: javascript代码如下:

var docs = {{ txtDocs | tojson }};
console.log(docs);

The Python code is the following: Python代码如下:

app = Flask(__name__)
@app.route('/')
def landingPage():
    return render_template('landingPage.html', txtDocs=json.dumps(os.listdir(pathToTxtDocs)))

I think that piece of JavaScript code inside the HTML is interpreted by Jinja2, while in case the js code is inside a file it is not interpreted. 我认为HTML内的那段JavaScript代码是由Jinja2解释的,而如果js代码在文件内,则它不会被解释。 I'm quite sure. 我很确定 But I would like to have separate html code and javascript code, for better reading. 但我想使用单独的html代码和javascript代码,以便更好地阅读。
So, is there a way to read a list sent by python to a js file? 那么,有没有办法读取python发送到js文件的列表? (Obviously linked in the HTML header). (显然链接在HTML标头中)。

Thanks in advance. 提前致谢。

I think that piece of JavaScript code inside the HTML is interpreted by Jinja2, while in case the js code is inside a file in itself it is not interpreted 我认为HTML内的一段JavaScript代码是由Jinja2解释的,而如果js代码本身在文件内,则它不会被解释

Indeed. 确实。

But I would like to have separate html code and javascript code, for better reading. 但我想使用单独的html代码和javascript代码,以便更好地阅读。

This only makes sense if your js code is used in more than one template - else having to open and read each js file linked in your template (directly or via a parent template) to find out what js code is actually executed really doesn't make your code more readable. 这仅在您的js代码用于多个模板中才有意义-否则必须打开并读取模板中链接的每个js文件(直接或通过父模板)来找出实际执行的js代码确实没有使您的代码更具可读性。

Now for the technical solutions: the first one is to have a function in your .js file and call it from the template - which means you'll STILL need to have some js in the template: 现在是技术解决方案:第一个是在.js文件中具有一个函数,然后从模板中调用它-这意味着您仍然需要在模板中包含一些js:

# yourlib.js

function doSomethingWithDocs(docs) {
    // your real code here
}

and

# template

<script src="/your/js/yourlib.js"></script>
<script>doSomethingWithDocs({{ txtDocs | tojson }});</script>

The other solution is to expose an API endpoint in your flask app that returns the docs as json already, and make a call to this endpoint in your js file. 另一种解决方案是在flask应用程序中公开一个API终结点,该终结点已经将文档返回为json,然后在js文件中调用此终结点。 You will very probably STILL need to pass some data from the view to the template to the js function to make the correct API call though... 您很可能仍然需要将一些数据从视图传递到模板到js函数,以进行正确的API调用。

IOW: your code is fine this way, it works, and it's as simple and obvious as possible. IOW:这样,您的代码就可以正常工作,并且尽可能简单明了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM