问题
1. Summary
I don't find, how I can get Python variable, use Grunt.
What I need to learn, that solve this task?
2. Details
I build site use Pelican and Grunt. I use the same variables:
- In Python format in
pelicanconf.py
for Pelican - In JavaScript format in
Gruntfile.coffee
for Grunt
I want that variables not to be duplicated and contains only in pelicanconf.py
.
3. Example
Part of pelicanconf.py
:
SASHAVARIABLE = 'sashavalue'
Part of Gruntfile.coffee
(I use template mechanism):
variables:
sashavariable: "sashavalue"
exampleplugin:
exampletask: "<%= variables.sashavariable %>"
I want to get value of SASHAVARIABLE
variable in pelicanconf.py
via Grunt.
At the time, if I change value of SASHAVARIABLE
, I need to change value in Gruntfile.coffee
too. This is a duplicate work.
4. Not helped
- 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.
Thanks.
回答1:
Directly reading/running Python in JavaScript isn't really something that is easily done.
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.
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).
Another common choice would be YAML, though you'll need a third-party library for it in Javascript.
来源:https://stackoverflow.com/questions/49901577/get-python-variable-using-grunt