Forums

How to run my python script from external web page

I've uploaded a python script to my pythonanywhere folder. I have also installed Flask. How do I need to modify that flask file to run this python code and then how can I get an external web page app to kick off that run? I understand python and HTML, but have never worked with Flask or curl, which is what I think I will have to use from that external web site.

See http://help.pythonanywhere.com/pages/Flask/

I guess I am a little dense. I've done what that site said, but do I then put a call to my python script in the def home() section of that WSGI file? What would the curl look like to run that?

Don't try to do everything at once. First get a working Flask app that you can see in your browser. There are tutorials and help on the PythonAnywhere help pages for that. Once you have that, try changing something minor, like the text on the front page. At that point you will understand quite a bit about web apps, so you can then work out how to incorporate your code into your web app.

I'm not really developing a web app. What I am trying to do is have an execution site for an existing, working python script which does a syntax check on a particular file type, where the files are stored in a github repository. What I want is to have the git hook for a pre-commit kick off the running of this python script. I have that git hook running a local version of that python script, but I need to have the ability to have any of our github users who do a commit, get this python syntax checker to run to establish whether the commit should complete or not. This syntax checker creates an HTML file if/with any issues found. This file is then brought up in a browser for that user to examine to determine what edits are needed for the next git add/ got commit. I've got everything running except for not having the capabillity avaliable to the other users who do not want to have python installed on their machines, since they are not "software types". Related to taking one step at a time, I need to initially get the Flask app to run this sntax checker. Is that as simple as adding a python system call to "python <that other script in my pythonanywhere folder>'? The next step would be to develop the curl command to run that Flask app.

You could call your script with a subprocess.call from your web app, but it would probably be better to import something from the script (a function, a class etc.) that does all the work of the script.

I'm a little confused about what the script is going to be working on, though. How are you envisioning it getting the code it's supposed to syntax check and how is it supposed to return its results, if not in the web app response?

I'm not that pythonic. I spent years with perl before I found python and got converted. I'm not sure exactly what you mean by import. Is there a way to treat this other script like a package and just make "calls" to its functions like it was all one big python script?

Since this is being kicked off by a pre-commit git hook from a repository, the first things that this script does is to get a list of the affected files from the specific git commit that was just issued. The script then traverses that list and "reads" those files from the repo to do the syntax check.

I'm not sure what you mean by the web app response. Currently, if there is a syntax issue the script writes an HTML file to the user's disk, then brings up their browser pointing to it, and then signals git by a non-zero return code NOT to complete the commit. Is there a cleaner way to do this HTML part in PythonAnywhere? If there are no issues, the script does nothing, except to signal git that it CAN complete the commit via a return code of "0".

To your first question: That's exactly what I meant. That's what import does. You're going to need to learn at least a little bit about Python if you want to make this work.

If it is being kicked of by the pre-commit hook, how are you planning to get the list of files to your web app? You'll probably need to send them in the request that you send from the pre-commit hook. In fact, you probably need to send the contents of the files in the request. Otherwise, how does the web app get the files?

Web apps take requests and return responses. That's the way that they work. Your curl in the pre-commit hook would make a request and the web app would return a response. I'm guessing your pre-commit hook will need to take the response and open the browser with the syntax issues. In that case, the response would need to contain the syntax issues that were found.

Glenn

When I said I didn't know about imports, what I was trying to say was that I was unaware of the capability to import local modules. I've been copy/pasting "utility" defs for too long. Thanks for the pointer.

The hook will have to build a string of info for the front-end python script. This will be the username and the modules affected. The PythonAnywhere front-end python will take that data and call the syntax check with the username and for each module, with those names enough to build a path to pass to httplib2 to open up the file under GitHub, which is the best solution. From talking to others curl can pass parameters. (I may need some coaching on that or a link to a good resource page, with examples, on curl. Google search can find the good, the bad, and the ugly.)

Relative to the response, I thought the front-end script could open a tempfile while the syntax checker script writes to it with the issues (in formatted HTML). When all files have been checked,, that front-end script would have to "stream" that HTML data back to the hook for it to display to the user. No data (empty) coming back indicates success so that the hook can let the commit complete

I think the tricky thing here will be the curl side. It would be really easy two write a Flask app that takes parameters, calls your code, then returns a response. If the status code of the response is a 200 (OK) for "no syntax errors" and some other response (say, a 400) when there are errors, then I believe curl will exit with status code 0 or non-zero appropriately.

Our Flask tutorial will help with the web app side of things; although it goes into some depth on stuff you don't need in the second half (databases and so on) the first parts focus on getting a simple website up and running, and being able to send it data and get responses.

But on the curl side I'm still a little unclear. I think my biggest point of confusion is that I don't understand where the curl command will run. Git hooks normally run on the computer where the repository to which you are committing or pushing lives. Which computer is this in your system?

Relative to your last point, our GitHub is on a remote server. The user goes to the repository there and uses its GUI to edit the file(s). At the bottom of the edit page is the Commit button. With the git hook, when they hit that it will kick off the hook on their machine. That hook is currently a bash script which gathers the user's GitHub forked repository address and the resource name of what was just committed. That hook would then use curl to send that info to the PythonAnywhere receiver and do its thing. I just realized that in this context of steps there will be only 1 resource that was modified for the commit.

I'm not sure now whether the HTML generated by the syntax checker would be displayed on your site, or whether that data is "pushed" back to the hook for it to display in s browser. It will then set its return code appropriately for git.

You mention "our GitHub" -- do you mean your own server running as a git server? Or are you using github.com?

Regarding where the HTML would be displayed -- that would be entirely up to you. As you have control of the curl call, perhaps you could write the results to a file locally and then tell the user's browser to display it?

The project GitHub is on https://git.door43.org. There are about 45 repos under https://git.door43.org/unfoldingWord/. It's sibling, WycliffeAssociates has only about 10, which end up being forks from the other. The team I am supporting is using https://git.door43.org/unfoldingWord/en_ugl.

As long as I can "push" an HTML stream of data back to the hook, I can then write that to a local file of the user and bring it up in their browser. I have the script written to look up the user's executing browser and save that browser's path to bring up the file under it. They WILL have a browser running since they had to have it to bring up the GitHub for their edits.

The way to "push" the HTML stream to the hook would be as the response from your web app.