使用Grunt获取Python变量 [英] Get Python variable using Grunt

查看:84
本文介绍了使用Grunt获取Python变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找不到,如何获取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:

  1. Pelican中pelicanconf.py的Python格式
  2. Grunt为Gruntfile.coffee的JavaScript格式
  1. In Python format in pelicanconf.py for Pelican
  2. 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.pySASHAVARIABLE变量的值.

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.

  1. 我尝试在Google中以
  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.

谢谢.

推荐答案

在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')

,并且基本上每种语言都有解析器(用于Python的 ).

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆