1

I have a Python script and a JS on my server. In the python script, I want to set the value of a variable to be the output of one of the functions in my .js file.

Is this possible, and if so how wold one go about doing this?

Thanks!

Bill the Lizard
407k213 gold badges579 silver badges892 bronze badges
asked Jul 3, 2010 at 14:21

4 Answers 4

4

As an alternative to what Macha proposes (an Ajax query) you could also create the Javascript file dynamically.

When the page is loaded, the javascript is loaded. In your page is the URL to the javascript in the form of

<SCRIPT LANGUAGE="JAVASCRIPT" SRC="mycode.js" TYPE="TEXT/JAVASCRIPT">

The trick is now to let the server not serve a static file "mycode.js" but the output of e.g. mycode.py. Your tag would thus look like

<SCRIPT LANGUAGE="JAVASCRIPT" SRC="mycode.py" TYPE="TEXT/JAVASCRIPT">

and the python script would look possibly like (simplified):

var_value = "whatever you want the variable to be"
jsfile = open("myscript.js", "rb")
for line in jsfile:
 print line.replace("$MYVAR$", var_value)
jsfile.close()

In short, you have your current js file, but you'd use it as a template for the python script. You substitute your variable with its value using replace.

It may not be the most elegant solution but it should work, and isn't all that difficult to understand.

Don't forget that your server should know how to handle python scripts :)

answered Jul 3, 2010 at 14:48
Sign up to request clarification or add additional context in comments.

4 Comments

Just one point: language="javascript" hasn't been needed for years. It's redundant with type="text/javascript".
Actually, all attributes except for src are unnecessary anymore.
@Justin Johnson even if the SRC points to a python script? That would mean that the contents of the URL gets analyzed to check what kind of code it contains.
I can't find the source anymore, but there was a conference video talking about how browsers typically just check the mimetype and ignore the attributes
1

It is not possible, as Javascript usually runs on the client side, while Python runs on the server side.

If it is code that needs to run on the client side, have it send an AJAX request to a python script with the results you need. If it does not need to run on the client side, I suggest you rewrite it in Python.

answered Jul 3, 2010 at 14:24

2 Comments

Thanks Macha! So could I have the script run client side and have the HTML or JS call python remotely? I could rewrite the python script to return a value, and I could call that from my HTML page or JS, right?
It would have to print the value out to a page, not just return it, for the Javascript to be able to read it.
1

You can simply print javascript blocks from your python script... and set variables in those blocks that are used from the external script.

<script language="javascript">var myvalue="data";</script>

using simplejson you can even print arrays and stuff like that.

Just make sure that the code in your javascript file is in a function that is called after the whole page is loaded or load the javascript file at the end of the page.

answered Jul 3, 2010 at 15:20

Comments

0

If you're using webpack, one way to share variables between python and javascript files without loading any http request is to store the variables in a serializable format like yaml or json and use yaml loader to parse it on the frontend.

const.yaml

DAY_MON: MON
DAYS:
 - MON
 - TUE
MAP_CONTENT_TYPE_TO_EXTENSION:
 image/gif: .gif
 image/jpg: .jpg

const.py

import yaml
globals().update(yaml.load(open('./const.yaml')))
print DAY_MON
print DAYS
print MAP_CONTENT_TYPE_TO_EXTENSION
# MON
# ['MON', 'TUE']
# { 'image/gif': '.gif', 'image/jpg': '.jpg' }

Install loader to parse yaml npm install yaml-loader

const.js

import data from "json-loader!yaml-loader!./const.yaml";
data.ANOTHER_VARIABLE = "SOME_VAR";
export default data;

read.js

import constants from './const.js';
console.log(constants.ANOTHER_VARIABLE);
console.log(constants.LISTING_TYPE_BRAND);
console.log(constants.DAYS);
console.log(constants.MAP_CONTENT_TYPE_TO_EXTENSION);
// SOME_VAR
// MON
// ['MON', 'TUE']
// { 'image/gif': '.gif', 'image/jpg': '.jpg' }
answered Jan 17, 2018 at 12:14

1 Comment

This works but you wont get autocompletion or static type checking. I wrote a tool to solve this by autogenerating code from yaml files: github.com/aantn/reconstant

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.