Reputation: 15
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!
Upvotes: 1
Views: 2977
Reputation: 466
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' }
Upvotes: 0
Reputation: 10433
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.
Upvotes: 1
Reputation: 23950
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 :)
Upvotes: 4
Reputation: 14654
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.
Upvotes: 1