0

I just needed a bit of guidance for what I should read more on for the following problem:

I have a few python scripts in a folder on my network drive that I run daily and I want to create a website where I display those scripts in form of a list and when I click on them I want them to be running.

Is there a way to solve this problem with AngularJs ?

Thanks for the help

7
  • Do you want to run the scripts on the server or on your local machine? Commented Jan 28, 2016 at 10:26
  • Be more specific, where you want run the web server? Where it should run after clicking?. Moreover it is not possible with AngularJS alone Commented Jan 28, 2016 at 10:28
  • ..possibly both. They will be stored on a network drive. I think in the beginning they will be running on some virtual machine. But it'd be nicer if they could run on a server. I am not sure how to solve this technically. But what I want is to run them from the website. So I guess I have to put them on a server. Commented Jan 28, 2016 at 10:29
  • 3
    on server you need PHP, Flask/Django/etc(Python), Ruby on Rails, etc. which can receive Angular request and can run Python script (or other program) on server. Your scripts has to be on server. You can put them manually or you can do webpage which can upload script on server. Commented Jan 28, 2016 at 10:43
  • 1
    Honestly, a small NodeJS server is all you need for this. Load up lists from AngularJS, probably a simple JSON file. Then just send those script paths from frontend to your node backend to run them. Promises might be required at the backend if you need data on the frontend. Commented Jan 28, 2016 at 10:49

1 Answer 1

1

Okay, So I have a small node server I which I changed a bit for your purpose, I know this isn't an ideal server, but this should do the job. The response on the post request should depend on weather you just want to fire the python script or you want its output too.

From frontend, send a post request with data containing the path to the python script or something else, and change the post case yourself.

Use an angular loop to read through the list, which can be a json served from the static folder in your directory.

This server is not secure by any means and probably can be cracked into in a short while, but for a local scenario it serves the purpose.

Also, if you don't wanna use this, then remember that you can server up the list from an endpoint to the page and populate the HTML using ng-repeat and then send post requests with either the script relative path or the script name and handle paths at the backend, this will be the takeaway. Sorry couldn't post anything for frontend.

var http = require('http');
var fs = require('fs');
var url = require('url');
var colors = require('colors');
var exec = require('child_process').exec;

var staticReg = /^\/static\/\w+\.\w+$/;;
var server = http.createServer(function(request, response){
    var url_parts = url.parse(request.url)
    if (request.method=="GET"){
        process.stdout.write("GET :: ".green + url_parts.path.yellow)
        if(staticReg.test(url_parts.path)){
            if (fs.existsSync("."+url_parts.path)){
                response.writeHead(200)
                response.write(fs.readFileSync("."+url_parts.path))
                response.end()
                process.stdout.write(" :: 200\n".green)
            }
            else{
                response.writeHead(404)
                response.end()
                process.stdout.write(" :: 404\n".red)
            }
            return
        }

        switch (url_parts.path){
            case "/":
                    process.stdout.write(" :: 200\n".green)
                    var home = fs.readFileSync("./index.html")
                    response.writeHead(200);
                    response.write(home);
                    response.end();
                    break;
            default :
                    process.stdout.write(" :: 404\n".red)
                    response.writeHead(404);
                    response.write("404 not found")
                    response.end();
                    break;
        }
    }

    if (request.method=="POST"){
        process.stdout.write("POST :: ".green + url_parts.path.yellow)
        var body = '';
        request.on('data',function(data){
            body+=data;
        });
        request.on('end', function(){
            switch(url_parts.path){
                case "/fire/":
                        body=JSON.parse(body)
                        process.stdout.write(" :: 200\n".green)
                        command='python '+body["script_path"]
                        exec(command,function callback(error, stdout, stderr){
                            // Do error checking here and send response.
                        })
                        // Or send response here.
                        response.writeHead(200);
                        response.write("Command fired.")
                        response.end();
                        break;
            }
        })
    }
})

server.listen(8002);
console.log("Listening to "+"localhost".blue+" on port "+ "8002".green);
server.on('error',function(err){
    console.log('Error happened: '+err.toString());
})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.