使用Grunt获取Python变量 [英] Get Python variable using Grunt
问题描述
我找不到,如何获取Python变量,请使用Grunt.
I don't find, how I can get Python variable, use Grunt.
解决这个任务需要学习什么?
What I need to learn, that solve this task?
I build site use Pelican and Grunt. I use the same variables:
- Pelican中
pelicanconf.py
的Python格式 - Grunt为
Gruntfile.coffee
的JavaScript格式
- In Python format in
pelicanconf.py
for Pelican - In JavaScript format in
Gruntfile.coffee
for Grunt
我希望变量不能重复并且仅包含在pelicanconf.py
中.
I want that variables not to be duplicated and contains only in pelicanconf.py
.
pelicanconf.py
的一部分:
SASHAVARIABLE = 'sashavalue'
Gruntfile.coffee
的一部分(我使用> 模板机制 > ):
Part of Gruntfile.coffee
(I use template mechanism):
variables:
sashavariable: "sashavalue"
exampleplugin:
exampletask: "<%= variables.sashavariable %>"
我想通过Grunt获取pelicanconf.py
中SASHAVARIABLE
变量的值.
I want to get value of SASHAVARIABLE
variable in pelicanconf.py
via Grunt.
当时,如果我更改SASHAVARIABLE
的值,我也需要更改Gruntfile.coffee
中的值.这是重复的作品.
At the time, if I change value of SASHAVARIABLE
, I need to change value in Gruntfile.coffee
too. This is a duplicate work.
- I try search queries in Google as
(grunt OR javascript) (get OR parse OR scrape) python variable
→ I don't find answer to my question.
谢谢.
推荐答案
在JavaScript中直接读取/运行Python并不是一件容易的事情.
Directly reading/running Python in JavaScript isn't really something that is easily done.
您可以将Python值作为字符串加载并手动解析.但是,我强烈建议您避免这样做,因为它要么要求您拥有一个非常简单的Python文件,要么就需要一个非常复杂的字符串解析器.
You could load the Python value as a string and manually parse it. However, I would highly recommend avoid doing this, as it either requires you to have a really simply Python file, or a really complicated string parser.
相反,我建议选择一种可以让两者容易读取的格式.在处理JavaScript时,JSON
始终是一个不错的选择,并且可以本地读取:
Instead, I'd recommend picking a format that can easily be read by both. JSON
is always a good choice when dealling with JavaScript and is can be read in natively:
const config = require('./path/to/config.json')
and there are parsers for basically every language (for Python).
另一个常见的选择是YAML,尽管您需要使用Javascript为其提供第三方库.
Another common choice would be YAML, though you'll need a third-party library for it in Javascript.
这篇关于使用Grunt获取Python变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!