can i use the browser to show the result of python

Dave Angel davea at ieee.org
Sun Sep 27 00:24:43 EDT 2009


Hacken wrote:
> On Sep 25, 6:27 pm, Dave Angel <da... at ieee.org> wrote:
>   
>> Hacken wrote:
>>     
>>> I have write some python script
>>>       
>>> i want to use browser(IE or FF) to call it, an show the returns!
>>>       
>>> how to?
>>>       
>> You don't say much about your environment, nor the nature of your
>> script. So my response will be very generic.
>>
>> If your script writes a valid html/xml/xhtml format to stdout, then you
>> could put the script onto a web server with cgi enabled, and mark it
>> executable.  Then you could enter the URL for that cgi file into your
>> browser, and see that generated web page.
>>
>> Interesting additional gotchas:  You need rights to upload (ftp) to such
>> a server.  The server needs to have an appropriate Python available, and
>> configured to permit cgi access for files with the .py extension.  
>> Further, if the server is Unix, your file must be in Unix text format,
>> with a shebang line that matches the location of the appropriate version
>> of python on that particular server.
>>
>> DaveA
>>     
>
>
> Thanks.
>
> but,i do not want to setup a webserver, i think that is so big for
> other user.
>
> i just want write my programes in python, and i use Browser to show my
> GUI,
>
> can i do that?and how to?
>
> thanks,waitting......
>
>   
What I described is all I've done firsthand.  But more is possible.  And 
I've worked on fancier setups, but somebody else did the plumbing.

As Stephen points out, you can use webbrowser module to launch a 
browser.  So you could write python code to create a web page(s), write 
it to a file, then launch the browser using the "file://......" 
protocol.  That'd be fine for displaying pages that are generated 
entirely before launching the browser.  But if you want the python 
program to get feedback from the browser (which is usually what's meant 
by using the browser for a GUI), you're going to have to simulate a 
webserver.

That can be done on a single machine using the "localhost" shortcut.  I 
don't know how to do it, but there is a sample in the 2.6 release of 
Python, at least in the Windows version.  You run it from the Start 
menu->Python2.6->module docs.

The source for the main server is in 
c:\Python26\Tools\Scripts\pydocgui.pyw.  (though all it does is import 
pydoc.py and call it.)  Now that particular program's purpose is to 
generate and display docs for the python files on the system, but 
presumably it's a starting place.

Now, this is the first I've looked at this file, but at line 1967 is a 
function serve(), which is commented as the web browser interface.   The 
function serve()  probably has much of what you're interested.  In 
particular, it contains a few class definitions, including DocServer and 
DocHandler, which send data back and forth to the browser, over the 
localhost connection.

It looks like it gets its core code from BaseHTTPServer module.

At line 2058 is a function gui(), which is a small tkinter program that 
just displays a few buttons and such.  That's not what you're asking about.

Hopefully somebody else has actually used some of this stuff, and can 
elaborate.

DaveA





More information about the Python-list mailing list