javascript - Create a hyperlink (or button) that executes a python script and then redirects when script completes -


alright, managed working how want (although some/most might disagree method). used flask recommended below. part might considered "wrong" 404 check loop. isn't proper design , results in "stuck script" error in browsers if goes on long. however, script want run doesn't last long enough problem.

thanks , please let me know if have other suggestions.

flask app:

import threading import subprocess import os import sys flask import flask flask import render_template, abort app = flask(__name__) app.debug = true  def run_script():     theproc = subprocess.popen([sys.executable, "run_me.py"])     theproc.communicate()  @app.route('/') def index():     return render_template('index.html')  @app.route('/generate') def generate():     threading.thread(target=lambda: run_script()).start()     return render_template('processing.html')  @app.route('/is_done') def is_done():     hfile = "templates\\itworked.html"     if os.path.isfile(hfile):         return render_template('itworked.html')     else:         abort(404)  if __name__ == "__main__":     app.run() 

processing.html:

<!doctype html> <html> <head>     <script src="/static/jquery.min.js"></script> </head>  <body> <p>processing...</p> <script>     setinterval(function()     {         var http = new xmlhttprequest();         http.open('head', "is_done", false);         http.send();         if (http.status==200)             window.location.replace("is_done");     },2000); </script> </body> </html> 

original question:

i beginner/intermediate python programmer , beginning web developer please gentle. want: user clicks hyperlink , taken intermediate web page says "processing...". in background (on server) link triggers python script processes file , creates new web page. on completion of script, user redirected newly created page. have: have script processing , spits out new page. haven't figured out how trigger python script , trigger redirect on script completion. i've looked @ web frameworks django, , cgi scripting. haven't found feel fits bill. it's possible i'm missing obvious i'd appreciate help. thanks.

a simple way tackle problem use simple web framework flask build web part uf system. in request handler magic link, need spawn script , keep track of it. simple way see if script done , relay user periodically send off ajax request check completion.

so example flask website like:

import threading import subprocess import uuid flask import flask flask import render_template, url_for, abort, jsonify, request app = flask(__name__)  background_scripts = {}  def run_script(id):     subprocess.call(["/path/to/yourscript.py", "argument1", "argument2"])     background_scripts[id] = true  @app.route('/') def index():     return render_template('index.html')  @app.route('/generate') def generate():     id = str(uuid.uuid4())     background_scripts[id] = false     threading.thread(target=lambda: run_script(id)).start()     return render_template('processing.html', id=id)  @app.route('/is_done') def is_done():     id = request.args.get('id', none)     if id not in background_scripts:         abort(404)     return jsonify(done=background_scripts[id]) 

and index.html:

<a href="{{ url_for('generate') }}">click me</a> 

and processing.html:

<html> <head> <script src="/static/jquery.js"></script> <script>     function ajaxcallback(data) {         if (data.done)             window.location.replace("http://your_generated_page_url");         else             window.settimeout(function() {                 $.getjson('{{ url_for('is_done') }}', {id: {{ id }} }, ajaxcallback);             }, 3000);     }     $(document).ready(function(){         ajaxcallback({done=false});     }); </script> </head> <body>     processing... </body></html> 

this untested code @ moment, hope idea on how approach problem. keep in mind work if serve page 1 process, if set apache , mod_wsgi, make sure there 1 process in process group.

if need more complex solution, might want @ message queues , like.


Comments

Popular posts from this blog

jquery - Invalid Assignment Left-Hand Side -

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -