Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

can i use the browser to show the result of python

0 views
Skip to first unread message

Hacken

unread,
Sep 25, 2009, 5:41:14 AM9/25/09
to
I have write some python script

i want to use browser(IE or FF) to call it, an show the returns!

how to?

Dave Angel

unread,
Sep 25, 2009, 6:27:33 AM9/25/09
to Hacken, pytho...@python.org
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

Hacken

unread,
Sep 26, 2009, 10:05:37 PM9/26/09
to


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......

Steven D'Aprano

unread,
Sep 26, 2009, 10:19:25 PM9/26/09
to
On Sat, 26 Sep 2009 19:05:37 -0700, Hacken wrote:

> 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......


In Python 2.6, you can use the webbrowser module.


--
Steven

Dave Angel

unread,
Sep 27, 2009, 12:24:43 AM9/27/09
to Hacken, pytho...@python.org
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


Hacken

unread,
Sep 28, 2009, 9:57:31 PM9/28/09
to
> DaveA- Hide quoted text -
>
> - Show quoted text -


Thaks both u guys

I find a way to do my work ,but i found a basic webserver is really
necessary.

First,i use webbrowser,Steven recommend,to call the browser open a
html file,my gui,in this html file i make a form and submit a GET
request with the paraments i need

second, i use BaseHTTPServer, to handle the GET request,parse the
request get the paraments i need,
and use these parament to finish my logic

thrid, use BaseHTTPServer make a response to the browser with a
formated html file with my result.

It's not complex like i think,because the BaseHTTPServer is not
complex

Thanks DaveA,a webserver,like you said,is a necessary,&Steven, your
webbrowser.

catafest

unread,
Oct 5, 2009, 5:16:37 AM10/5/09
to

Python script running under web browsers only like:
python + website = django .

Hacken

unread,
Oct 10, 2009, 5:13:21 AM10/10/09
to

u can say that again.

0 new messages