user2023413
user2023413

Reputation:

using python and node.js

I use to program on python. I have started few months before, so I am not the "guru" type of developer. I also know the basics of HTML and CSS.

I see few tutorials about node.js and I really like it. I cannot create those forms, bars, buttons etc with my knowledge from html and css.

Can I use node.js to create what user see on browser and write with python what will happen if someone push the "submit" button? For example redirect, sql write and read etc.

Thank you

Upvotes: 2

Views: 4822

Answers (3)

user568109
user568109

Reputation: 47993

You can call python scripts in the back end at the node server, in response to button click by user. For that you can use child_process package. It allows you to call programs installed on your machine.

For example here is how to run your script when user POST's something on /reg page:

app.post('/reg', function(request, response){
      spawn = require('child_process').spawn;
      path = "location of your script";
      // create child process of your script and pass two arguments from the request
      backend = spawn('python',[path, request.body.name, request.body.email]);
      backend.on('exit', function(code) {
           console.log(path + ' exited with code ' + code);
           if(code==0)
           response.render('success'); //show success page if script runs successfully
           else
           response.redirect('bad');
      });    
});

Python has to be installed in your system, along with other python libraries you will need. It cannot respond / redirect to requests to node, else why would you use node then. When in Rome, do as the Romans do. Use JavaScript in node, calling external programs is not as fast using JS libraries.

Upvotes: 5

Blender
Blender

Reputation: 298096

Node.js is a serverside JavaScript environment (like Python). It runs on the server and interacts with the database, generates the HTML that the clients see and isn't actually directly accessed by the browser.

Browsers, on the other hand, run clientside JavaScript directly.

If you want to use Python on the server, there are a bunch of frameworks that you can work with:

Upvotes: 3

I think you're thinking about this problem backwards. Node.js lets you run browser Javascript without a browser. You won't find it useful in your Python programming. You're better off, if you want to stick with Python, using a framework such as Pyjamas to write Javascript with Python or another framework such as Flask or Twisted to integrate the Javascript with Python.

Upvotes: 0

Related Questions