2

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.

Саша Черных
  • 2,561
  • 4
  • 25
  • 71

1 Answers1

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.

samanime
  • 25,408
  • 15
  • 90
  • 139