Get Python variable using Grunt

て烟熏妆下的殇ゞ 提交于 2019-12-11 02:37:36

问题


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:

  1. In Python format in pelicanconf.py for Pelican
  2. 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

  1. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!