简体   繁体   English

Python Flask 应用程序:如何访问 java 脚本文件中的容器环境变量

[英]Python Flask App: how to access container environment variables inside java script file

i'm programming a python flask app which has some java script files.我正在编写一个 python flask 应用程序,它有一些 java 脚本文件。 There are some environment variables that were defined when the container is deployed to kurbenetes.在将容器部署到 kurbenetes 时定义了一些环境变量。 How to access this variables for a java script file inside an flask application?如何在 flask 应用程序中访问 java 脚本文件的此变量?

In Python it would be with os.environ['Variable'] = 'some_value' .在 Python 中,它将与os.environ['Variable'] = 'some_value' How about javascript? javascript怎么样?

Thank you谢谢

I am sure there are several viable solutions, but I would use data attributes .我确信有几种可行的解决方案,但我会使用data attributes

So basically you render the variables via your templates as HTML, and then access them in your JavaScript.所以基本上你通过模板将变量渲染为 HTML,然后在 JavaScript 中访问它们。

Example from the linked article链接文章中的示例

<article
  id="electric-cars"
  data-columns="3"
  data-index-number="12314"
  data-parent="cars">
...
</article>

And then you'd access the element and its attributes via JavaScript.然后您将通过 JavaScript 访问元素及其属性。

As an alternative, you could generate the JavaScript code from your backend.作为替代方案,您可以从后端生成 JavaScript 代码。

As a third way, you could call your backend via http from your JavaScript code.作为第三种方式,您可以通过 http 从您的 JavaScript 代码调用您的后端。

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

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