How can I execute a Python script on a webpage?
I've used XAMPP to create the Apache server. Are there any tutorials/examples or guides on how to execute a .py script? I'm using Windows 7 and have installed Python on my local machine. If I access the .py script via the web link, it looks as if its HTML code and nothing is executed.
Typically, you don't execute Python in the browser. Instead, the browser accesses a resource (or "webpage", like http://example.com/mypage) by requesting the resource from the server. The server (for example, Apache), when administered correctly, passes off handling of the request to some Python script. Then, your Python script creates some output (for example, HTML) which the server then returns to the browser for the browser to display.
However, some web sites have found it useful to have logic (scripts) run in the browser, rather than on the server. The standard way of doing this is using JavaScript (although in the past there WERE other languages built into browsers, such as VBScript in Internet Explorer).
Right now, pretty much all browsers have settled on JavaScript as THE scripting language in the browser. In order for you to use any other language in the browser (including Python), the browser must support that scripting language (or needs to have an add-on to support that scripting language). Simply having Python installed on your client alongside the browser is not enough. For more information, please see the Python documentation Web Browser Programming.
Another option is to use something like Pyjs. This is a library that has you write your code in Python, and converts the necessary parts to JavaScript. This isn't exactly "Python in the browser", but it might be something you are looking for.
I guess this links will help....
https://community.apachefriends.org/f/viewtopic.php?t=42975
Usually a good resource for Python is the official documentation.
They do a great job of explaining many aspects of Python. Using Python on the web is a big part and they have a great overview.
Related
I'm trying to integrate backend code into a Wix site. Im not too picky about how I want to do this, or what language to write in (ideally, I have a locally-hosted Java code that I'd love to simply call). I wouldn't mind re-writing it in JavaScript though, or another language. But before I decide that I'm confused about my options. I can code but I'm new to the concepts like modules, APIs, & servers.
According to my research, back-end code with Wix is supposed to be easy (or at least do-able and not THAT complicated)....
From this webpage https://support.wix.com/en/article/corvid-calling-server-side-code-from-the-front-end-with-web-modules,
"Web modules are exclusive to Corvid and enable you to write functions that run server-side in the backend, and easily call them in your client-side code. With web modules you can import functions from backend into files or scripts in public, knowing they will run server-side. Corvid handles all the client-server communication required to enable this access."
And from this: https://www.sitepoint.com/what-is-wix-code/
"It’s serverless: All this added functionality comes in a serverless environment that lets you get your work done without any of the normal full-stack development headaches.
Just code and go: Wix Code has a built-in, online IDE and backend so you can just add the code you need to your page or your site, publish, and you’re live."
So, I thought they have a backend IDE where I can write backend code directly, or I could call my Java program. But, as I tried doing this and finding tutorials, it seems I can really only do this by calling a public API from the backend...?
https://youtu.be/tuu0D1izrUU
But ive also read (and someone who supposedly has done it before told me this) that Wix integrates with node.js, which is a backend version of JavaScript.
Can I use a Wix domain for a NodeJS app?
But, when I go into my Wix site I cannot find any option for using Node JS, and doing research on that gives me no useful results.
So, I'm thoroughly confused on what the capabilities are here. Can someone help me make sense of this?
Why are there no tutorials showing explicit code in the Corvid backend module? What's stopping me from simply writing my Java program there in a module? Do I really need an API endpoint to call and pass to the front end?
Is Node JS supported or not - has anyone done this before?
Also, in one link above they said everything is "serverless". But if I have to set up my own API endpoint won't I need to set up my own server??
There are basically two ways to go about this, which you seem to have already discovered.
Write your backend code in your Wix site. Indeed, the backend is built on Node.js as you can see here. Using this approach you will have to use JavaScript. As you seem to have found, you write this code in the Backend section of your site in a Web Module. Pros: you don't need to worry about managing a server and all your code is in one place.
Expose your already existing Java code as an API that your Wix site can call using the wix-fetch API. Pros: you don't need to rewrite your code.
Need advice on how to incorporate Python into an Azure ASP.NET web application environment. Please excuse this question but I am new to Azure and I'm not clear on how to proceed. Every option that I look into looks promising but they all seem to have their own issues. Below is a more thorough explanation but the deal is that I have an Azure account with all kinds of goodies, a full fledged ASP.NET (C#) web app running via App Service, I am new to Azure (but not Python), and I'm hoping to add Python functionality to this whole setup. In short:
I want to add Python to this setup mainly to run scheduled jobs and also to trigger Python code from ASP.NET web form submissions
ideally I want a solution that resembles a non-cloud setup. I know this sounds silly but I'm finding the cloud/Azure functionality to be nuanced and not straightforward. I want a place to put a bunch of Python scripts, run, edit, schedule and trigger them from ASP.NET
for example: I created a WebJob that runs manually and from the documentation it wasn't clear how it should be called. I just figured out that you need to POST with Basic Auth (and the credentials provided).
!Also, Azure CMD does NOT like files with 'underscore _' in them! You cannot submit a Web Job with a py file with an underscore nor can you write output with a file with an underscore
!Also, I don't see an option for this Web Job to run Python 3.6.4 (which I installed via extension). Right now it is using 2.7.15...
!Also, CRON expression in Azure has six *, not five plus a command. Again, more weird stuff to worry about
I tried these instructions but the updates to the web page's Web.config file breaks the ASP.NET web pages
ideally the most cost effective option
Any info is greatly appreciated
MORE DETAILED EXPLANATION
Currently I have an ASP.NET site running via Azure App Service and I would like to add Python scripts and possibly Flask/Rest functionality. Note that I am not expecting to serve any content via Python and will largely be running Python scripts either on a scheduled basis or call them from ASP.NET. As a matter of fact, and this is an important point, I'm hoping to have ASP.NET trigger/run a Python script when a web form is submitted. I realize that I could get a similar effect if I make a web call to a Rest api that is running Python. In any event, I can't tell if I should:
add a Python extension to the current App Service running the web page (I tried this) OR
I did install Python 3.6.4 and some packages via pip
These instructions were useful, however the updates to the web page's Web.config file breaks the ASP.NET web pages
set up a VM that will have all of the Python code (but how can I have the .NET web page(s) call the Python in the VM?) OR
use Azure functions (I'm completely new to this and must admit that I prefer to have my old school Python environment instead although I see the benefit of using functions. But how do you deal with logging and debugging?)
or what about a custom windows container (Docker)?
This requires installing VS Code and that is OK but I'm looking for a solution that another user can get into with as few interruptions as possible
The idea is to ramp up the use of Python although, like I said, I don't expect Python to be serving any of the web content. It will be used to run in the background and to run scheduled jobs. What is the most robust and hopefully easiest way to add Python functionality to Azure (most importantly in a way to be able to trigger/use Python from an App Service running .NET?)? I've searched online and stack overflow so far with interesting finds but nothing to my liking.
For example, the following link discusses how to schedule WebJobs. I just created a manual one and when I called the webhook I got the message: "No route registered for '/api/triggeredwebjobs/TestPython/run'" How to schedule python web jobs on azure
The Docker method looks very promising, however, I'm looking for a simple solution as there is another person who will be involved in all of this and he's busy with other projects
Thank you very much!
I found a solution, though I'm open to more info. Like I mentioned in my post, I used the 'add extension' tool to add Python 3.6.4 to my Azure (installed in D:\home\python364x64).
Then I installed a bunch of packages via pip, these installed into D:\home\python364x64\Lib\site-packages.
I created a Python folder in webpages\Python where I put my scripts.
Finally, in ASP.NET I used the Diagnostics.Process call to run my code in ~\webpages\Python\somecode_2.py
The main issue is that Azure came with Python 2.7.15 installed. And for some reason when my Python code got executed it was using 3.4 (where that version came from beats me). So for each script, I had to create an _2.py version where I simply did the following in order to call the original script via Python 3.6.4. Looks a little nasty but it works. So like I said, I would welcome more info for ways to do this better...
--
import os<br>
os.system("D:\\home\python364x64\python.exe SomePython.py {0}".format(add arguments here)
There is a Python interpreter in naclports (to run as Google Chrome Native Client App).
Are there any examples for bundling the interpreter with a custom Python application and how to integrate this application with a HTML page?
The interpreter is currently the only python example in naclports. However, it should be possible to link libpython into any nacl binary, and use it just as you would embed python in any other C/C++ application. A couple of caveats: you must initialize nacl_io before making any python calls, and as you should not make python calls on the main (PPAPI) thread.
In terms of interacting with the HTML page, as with all NaCl applications this must be done by sending messages back and forth between native and javascript code using PostMessage(). There is no way to directly access the HTML or JavaScript from native code.
I finished up some python tutorials and would like to go a bit further. I can open the IDLE and execute the code just fine by pressing f5 (save and run) on my desktop but that is the limit of my abilities. I would like to be able to execute the programs on a webpage
I tried simply uploading the file to my server, then browsing to it in chrome. I'm sure you know what happened: the url displayed text on the screen.
Since I am brand new to python, I am not sure where to start or even what questions to ask. Basically I would like to run the program in the browswer as if the browswer was the IDLE, or better yet, create an html/css button that runs the program when clicked.
I'd advise you to look into something like flask. It's a micro-framework that includes a basic web server. The documentation should get you most of the way that you want to go.
Running python on the web usually means that when you hit an URL, the server runs some kind of python script, and returns a string - the HTML content of the page you're requesting. You can't use python to 'script' the webpage as you'd use javascript.
You can run python in an interactive interpreter running within a webpage though - just check out try ipython.
Flask is good.
Cherrypy - http://www.cherrypy.org/ - is also a great choice for a really simple way to run python on the server.
Fundamentally, you just configure your web server to execute the file instead of display it. Typically you set this for *.py files but you could restrict it, say, to files in a particular directory. Apparently, your server already has such a setting for PHP files.
Wrapping Python with PHP (obviously) adds neither speed nor security or utility.
Down the line you might want to look at frameworks, mod_python, WSGI, etc, but for your immediate problem, those are severe overkill.
This is limited to static server-side code; JavaScript runs interactively in the visitor's browser, allowing for much richer user interaction. A server-side script runs when the browser attempts to load the page, and the page load finishes when the script is done. If you want something like IDLE in your browser, that's a JavaScript challenge rather than a Python task (and that particular wheel has already been invented, productized, marketed, and sold to the Americans: http://pythonfiddle.com/)
like this:
python -c "import urllib2; exec urllib2.urlopen("http://localhost:8000/test.py").read()"
I am basically a PHP guy. now moving towards python. I am starting to learn python.
How do I install it and start working it, and develop websites .
I got totally confused with the alternative implementations in the download section of the Python site. Can you tell me what "alternative implementations" means?.
I mean to say: I can create a .php file in my server and then access it from browser like http://example.com/index.php, so I was wondering whether I can do the same with python, like creating a .py file and accessing from browser http://example.com/index.py.
Just as a disclaimer, I interpret you saying "run Python in a browser" as "making a website with Python."
If you want to start writing web applications in Python, you can either use CGI or use one of its many web app frameworks. Python is not like PHP in the sense that you can't just embed it in HTML. Many of those frameworks come with development servers that you can use to test your web app (by looking at it in a browser).
A particularly good Python web framework is Django.
I really do recommend that you do the Python tutorial before you dive into any of those frameworks, though. Python is not only for writing web applications, so you'll have to get some fundamentals down first before any of that makes sense to you.
As for installing Python, I recommend you take the version you got with your OS if you use Mac or Linux, or installing the Python 2.7.1 32-bit binary from python.org if you use Windows. The alternative implementations include a Python that runs on the Java virtual machine and one that runs on the .NET Common Language Runtime, but for your purposes the reference implementation, CPython, should work fine.
Python by default is a general purpose scripting language and is not meant specifically for web application development (like PHP is primarily). So, you'll first need to download and install Python (choose the version that's appropriate for your OS) from www.python.org I'd recommend v2.7.1 but you can try out v3.2 if you'd like.
The other versions (IronPython, Jython, etc) are Python implementations on other platforms (.NET and JVM respectively), and in all likelihood you don't need to bother with them unless you really want to.
To start working with Python and create web applications you will also need to download and install a Python-based web framework. There are many of them, too many in fact to list here. However, there is a page on the Python Wiki that has a list of useful frameworks for web development.
You don't "run it from a browser". You don't run PHP from a browser, either.
The way to use Python for web development is to use a framework like Django or Pyramid.
Sage or CodeNode let you run Python in a browser.
You can get a limited Python experience by pointing your browser to http://shell.appspot.com/ . You will be running Python 2.5.2 on Google's site.