0

I am new in using Electron JS. Consider I have variable a="Hello World" in python file. I want to use it in my main.js file and display it in index.html file. How can I achieve this? Any help is appreciated...

1 Answers1

0

js and python are different programming languages. They don't understand each other. You can spawn a small process in js that executes a small python program which prints out the python variable you want. That output can be read in javascript.

Let's assume you have a python file named myprogram.py with the content:

foo="bar"

It is located in C:\tmp

You can write a command:

python -c "import os; os.chdir('C:\\tmp'); import myprogram; print(myprogram.foo)"

In node js you can execute the command and read the output, see: Execute and get the output of a shell command in node.js


Of course there are different and better ways of making js and python communicate. E.g. through REST APIs. But you explicitely said about reading a Python variable directly and not through the use of creating an API and utilizing that API.
If you want to read into that, look into Flask, Django, FastApi, CherryPy etc.

Those solutions requires an always running Python process alongside your node project.

Tin Nguyen
  • 5,250
  • 1
  • 12
  • 32