can i use the browser to show the result of python

Discussion in 'Python' started by Hacken, Sep 25, 2009.

  1. Hacken

    Hacken Guest

    I have write some python script

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

    how to?
    Hacken, Sep 25, 2009
    #1
    1. Advertising

  2. Hacken

    Dave Angel Guest

    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
    Dave Angel, Sep 25, 2009
    #2
    1. Advertising

  3. Hacken

    Hacken Guest

    On Sep 25, 6:27 pm, Dave Angel <> 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......
    Hacken, Sep 27, 2009
    #3
  4. 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
    Steven D'Aprano, Sep 27, 2009
    #4
  5. Hacken

    Dave Angel Guest

    Hacken wrote:
    > On Sep 25, 6:27 pm, Dave Angel <> 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
    Dave Angel, Sep 27, 2009
    #5
  6. Hacken

    Hacken Guest

    On Sep 27, 12:24 pm, Dave Angel <> wrote:
    > Hacken wrote:
    > > On Sep 25, 6:27 pm, Dave Angel <> 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- 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.
    Hacken, Sep 29, 2009
    #6
  7. Hacken

    catafest Guest

    On Sep 25, 12:41 pm, 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?


    Python script running under web browsers only like:
    python + website = django .
    catafest, Oct 5, 2009
    #7
  8. Hacken

    Hacken Guest

    On Oct 5, 5:16 pm, catafest <> wrote:
    > On Sep 25, 12:41 pm, 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?

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


    u can say that again.
    Hacken, Oct 10, 2009
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?YmlsZG8=?=
    Replies:
    4
    Views:
    409
    =?Utf-8?B?YmlsZG8=?=
    Nov 13, 2004
  2. Al Dykes
    Replies:
    1
    Views:
    232
    Tim Churches
    Nov 3, 2004
  3. J.Ram
    Replies:
    7
    Views:
    649
  4. Pavel
    Replies:
    7
    Views:
    524
    Pavel
    Sep 19, 2010
  5. Michael Tan
    Replies:
    32
    Views:
    958
    Ara.T.Howard
    Jul 21, 2005
Loading...

Share This Page