Run python script in node -webkit startup (nw.js)

I have a Python based application that controls LEDs. It uses Flask to build a web server, so I can work with a good user interface using HTML, CSS and JS.

My current process:

  • python home.py
  • go to localhost:5000 in browser
  • profit!

I would like to take another step and package it as an nw.js application (formerly node-webkit ).

Basically, it seems to me that I just need to run my python script before the window loads, so that my tested and correct flask web server starts and creates a localhost:5000 page for my nw.js application nw.js to open.

How can I pack an nw.js application so that it runs a python script at startup?

+5
source share
2 answers

You can create a download page and display the actual application after starting the web server.

package.json:

 { "name": "pythonApp", "version": "0.0.0", "main": "loading.html", "window": { "title": "App Name", "width": 800, "height": 600, "position": "center", } } 

The download page (load.html) will load the js file, which will launch your actual application page in the form of a hidden window, and you can show it when the server is running.

loading.html:

 var gui = require('nw.gui'); var currentWindow = gui.Window.get(); // Get reference to loading.html var exec = require('child_process').execFile; exec('home.py', {cwd:'.'}, function (error, stdout, stderr){ // Runs your python code var appWindow = gui.Window.open('app.html', // Starts your application { width: 800, height: 600, position: 'center', focus: false, transparent: true // This hides the application window } ); appWindow.on('loaded', function() { // Waits for application to be loaded currentWindow.close({force: 'true'}); // Closes loading.html appWindow.show(); // Shows app.html appWindow.focus(); // Set the focus on app.html }); }); 

In any case, this is the essence, but you may need to make changes for your specific installation. Hope this helps.

+3
source

Use the node child_process module and call it from index.html.

Sort of:

 <!DOCTYPE html> <html> <head> </head> <body> <script> var child_process= require('child_process'); child_process.spawn('python home.py'); </script> </body> </html> 

(Code not verified, just an example)

0
source

Source: https://habr.com/ru/post/1215054/


All Articles