can i use the browser to show the result of python

H

Hacken

I have write some python script

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

how to?
 
D

Dave Angel

Hacken said:
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
 
H

Hacken

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

Steven D'Aprano

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

Dave Angel

Hacken said:
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
 
H

Hacken

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

catafest

I have write some python script

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

how to?

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top