Dynamic HTML from Python Script

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • asdf

    Dynamic HTML from Python Script

    I have a python script whose output i want to dynamically display
    on a webpage which will be hosted using Apache. How do I do that?


    thanks
  • Aidan

    #2
    Re: Dynamic HTML from Python Script

    asdf wrote:
    I have a python script whose output i want to dynamically display
    on a webpage which will be hosted using Apache. How do I do that?
    >
    >
    thanks
    Well, there's a few ways you could approach it.

    You could create a cgi program from your script - this is probably the
    solution you're looking for.

    You could have the script run periodically and create a static html file
    in the webroot... this would be acceptable, maybe preferable, if the
    output from your script doesn't change frequently.

    There's also more advanced ways you can make python code run in a
    web-service. Cherrypy comes to mind, as well as the myriad python MVC
    frameworks.

    Comment

    • asdf

      #3
      Re: Dynamic HTML from Python Script

      Well, there's a few ways you could approach it.
      >
      You could create a cgi program from your script - this is probably the
      solution you're looking for.
      >
      Output from the script does come up very often. There is a new output
      every 10 secs and it's possible that the script might be run indefinitely.
      Basically I want all that output displayed in a web browser
      You could have the script run periodically and create a static html file
      in the webroot... this would be acceptable, maybe preferable, if the
      output from your script doesn't change frequently.
      >

      Comment

      • Aidan

        #4
        Re: Dynamic HTML from Python Script

        asdf wrote:
        >Well, there's a few ways you could approach it.
        >>
        >You could create a cgi program from your script - this is probably the
        >solution you're looking for.
        >>
        >
        Output from the script does come up very often. There is a new output
        every 10 secs and it's possible that the script might be run indefinitely.
        Basically I want all that output displayed in a web browser
        Well, in that case you could simply append the new output to a static
        file every 10 seconds, or whenever there is new output. That way, you
        just need to refresh the static file in your browser to see updates...
        Given what I understand of your situation, that's how I'd do it.

        A constantly running CGI app is probably not the best idea, given
        timeouts and other such constraints you might run into.

        >You could have the script run periodically and create a static html file
        >in the webroot... this would be acceptable, maybe preferable, if the
        >output from your script doesn't change frequently.
        >>

        Comment

        • Larry Bates

          #5
          Re: Dynamic HTML from Python Script

          asdf wrote:
          I have a python script whose output i want to dynamically display
          on a webpage which will be hosted using Apache. How do I do that?
          >
          >
          thanks
          Take a look at Django. It may be overkill for this first project but any time
          you spend learning it should be paid back in future projects.

          -Larry

          Comment

          • asdf

            #6
            Re: Dynamic HTML from Python Script

            On Wed, 11 Jun 2008 11:20:48 +1000, Aidan wrote:
            asdf wrote:
            >>Well, there's a few ways you could approach it.
            >>>
            >>You could create a cgi program from your script - this is probably the
            >>solution you're looking for.
            >>>
            >>>
            >Output from the script does come up very often. There is a new output
            >every 10 secs and it's possible that the script might be run
            >indefinitely . Basically I want all that output displayed in a web
            >browser
            >
            Well, in that case you could simply append the new output to a static
            file every 10 seconds, or whenever there is new output. That way, you
            just need to refresh the static file in your browser to see updates...
            Given what I understand of your situation, that's how I'd do it.
            >
            The problem with this is that browser would have to be refreshed manually
            every 10 seconds. Unless there is a way to set this in the script itself.

            A constantly running CGI app is probably not the best idea, given
            timeouts and other such constraints you might run into.
            >
            >
            >>You could have the script run periodically and create a static html
            >>file in the webroot... this would be acceptable, maybe preferable, if
            >>the output from your script doesn't change frequently.
            >>>

            Comment

            • Aidan

              #7
              Re: Dynamic HTML from Python Script

              asdf wrote:
              On Wed, 11 Jun 2008 11:20:48 +1000, Aidan wrote:
              >
              >asdf wrote:
              >>>Well, there's a few ways you could approach it.
              >>>>
              >>>You could create a cgi program from your script - this is probably the
              >>>solution you're looking for.
              >>>>
              >>>>
              >>Output from the script does come up very often. There is a new output
              >>every 10 secs and it's possible that the script might be run
              >>indefinitel y. Basically I want all that output displayed in a web
              >>browser
              >Well, in that case you could simply append the new output to a static
              >file every 10 seconds, or whenever there is new output. That way, you
              >just need to refresh the static file in your browser to see updates...
              >Given what I understand of your situation, that's how I'd do it.
              >>
              The problem with this is that browser would have to be refreshed manually
              every 10 seconds. Unless there is a way to set this in the script itself.
              You should be able to do that with just:

              <meta http-equiv="refresh" content="10"/>

              in the <headsection of your page (you can adjust the value of content
              from 5 to however many seconds you want between refreshes).

              You could also look at adding some AJAX-yness to your page, and have it
              query your script for new output every 10 seconds, and then add that
              content to the existing page... it sounds like this behavior is what
              you're looking for, but it's slightly harder to pull off than the method
              mentioned above.
              >
              >A constantly running CGI app is probably not the best idea, given
              >timeouts and other such constraints you might run into.
              >>
              >>
              >>>You could have the script run periodically and create a static html
              >>>file in the webroot... this would be acceptable, maybe preferable, if
              >>>the output from your script doesn't change frequently.
              >>>>
              >

              Comment

              • Aidan

                #8
                Re: Dynamic HTML from Python Script

                Aidan wrote:
                asdf wrote:
                >On Wed, 11 Jun 2008 11:20:48 +1000, Aidan wrote:
                >>
                >>asdf wrote:
                >>>>Well, there's a few ways you could approach it.
                >>>>>
                >>>>You could create a cgi program from your script - this is probably the
                >>>>solution you're looking for.
                >>>>>
                >>>>>
                >>>Output from the script does come up very often. There is a new output
                >>>every 10 secs and it's possible that the script might be run
                >>>indefinitely . Basically I want all that output displayed in a web
                >>>browser
                >>Well, in that case you could simply append the new output to a static
                >>file every 10 seconds, or whenever there is new output. That way, you
                >>just need to refresh the static file in your browser to see updates...
                >>Given what I understand of your situation, that's how I'd do it.
                >>>
                >The problem with this is that browser would have to be refreshed manually
                >every 10 seconds. Unless there is a way to set this in the script itself.
                >
                You should be able to do that with just:
                >
                <meta http-equiv="refresh" content="10"/>
                >
                in the <headsection of your page (you can adjust the value of content
                from 5 to however many seconds you want between refreshes).
                To be clear, you can set the content attribute value to any arbitrary
                integer value.
                >
                You could also look at adding some AJAX-yness to your page, and have it
                query your script for new output every 10 seconds, and then add that
                content to the existing page... it sounds like this behavior is what
                you're looking for, but it's slightly harder to pull off than the method
                mentioned above.
                >
                >>
                >>A constantly running CGI app is probably not the best idea, given
                >>timeouts and other such constraints you might run into.
                >>>
                >>>
                >>>>You could have the script run periodically and create a static html
                >>>>file in the webroot... this would be acceptable, maybe preferable, if
                >>>>the output from your script doesn't change frequently.
                >>>>>
                >>

                Comment

                • dusans

                  #9
                  Re: Dynamic HTML from Python Script

                  On Jun 11, 1:58 am, asdf <a...@asdf.comw rote:
                  I have a python script whose output i want to dynamically display
                  on a webpage which will be hosted using Apache. How do I do that?
                  >
                  thanks
                  def index(req):
                  return "Page"

                  u cant run it on lighttpd also, which is much faster then Apache :P

                  Comment

                  • Lie

                    #10
                    Re: Dynamic HTML from Python Script

                    On Jun 11, 9:16 am, asdf <a...@asdf.comw rote:
                    On Wed, 11 Jun 2008 11:20:48 +1000, Aidan wrote:
                    asdf wrote:
                    >Well, there's a few ways you could approach it.
                    >
                    >You could create a cgi program from your script - this is probably the
                    >solution you're looking for.
                    >
                    Output from the script does come up very often. There is a new output
                    every 10 secs and it's possible that the script might be run
                    indefinitely. Basically I want all that output displayed in a web
                    browser
                    >
                    Well, in that case you could simply append the new output to a static
                    file every 10 seconds, or whenever there is new output.  That way, you
                    just need to refresh the static file in your browser to see updates...
                    Given what I understand of your situation, that's how I'd do it.
                    >
                    The problem with this is that browser would have to be refreshed manually
                    every 10 seconds. Unless there is a way to set this in the script itself.
                    Surely you don't think you can do that without Javascript don't you?
                    You can't make the browser refresh automatically in the server side,
                    it has to be done in the client side scripting or like Opera browser
                    that have an option to make it refresh a page every few seconds.
                    A constantly running CGI app is probably not the best idea, given
                    timeouts and other such constraints you might run into.
                    >
                    >You could have the script run periodically and create a static html
                    >file in the webroot... this would be acceptable, maybe preferable, if
                    >the output from your script doesn't change frequently.
                    >
                    >

                    Comment

                    • Ivan Illarionov

                      #11
                      Re: Dynamic HTML from Python Script

                      On Wed, 11 Jun 2008 01:05:45 +0000, asdf wrote:
                      >Well, there's a few ways you could approach it.
                      >>
                      >You could create a cgi program from your script - this is probably the
                      >solution you're looking for.
                      >>
                      >>
                      Output from the script does come up very often. There is a new output
                      every 10 secs and it's possible that the script might be run
                      indefinitely. Basically I want all that output displayed in a web
                      browser
                      Here's a simplified Django and AJAX solution:


                      1. Install Django http://www.djangoproject.com/documentation/install/
                      and choose the place to strore your Django apps

                      2. Run 'python django-admin.py startproject YOURPROJECTNAME '

                      3. Create views.py file inside YOURPROJECTNAME directory
                      with something like this:

                      from datetime import datetime
                      from django.http import HttpResponse
                      # import your script here


                      def myscript(reques t):
                      output = """\
                      <html>
                      <body>

                      <script type="text/javascript">
                      function ajaxFunction()
                      {
                      var xmlHttp;
                      try
                      {
                      // Firefox, Opera 8.0+, Safari
                      xmlHttp=new XMLHttpRequest( );
                      }
                      catch (e)
                      {
                      // Internet Explorer
                      try
                      {
                      xmlHttp=new ActiveXObject(" Msxml2.XMLHTTP" );
                      }
                      catch (e)
                      {
                      try
                      {
                      xmlHttp=new ActiveXObject(" Microsoft.XMLHT TP");
                      }
                      catch (e)
                      {
                      alert("Your browser does not support AJAX!");
                      return false;
                      }
                      }
                      }
                      xmlHttp.onready statechange=fun ction()
                      {
                      if(xmlHttp.read yState==4)
                      {
                      document.getEle mentById("hello ").innerHTML=xm lHttp.responseT ext;
                      setTimeout('aja xFunction();', 1000);
                      }
                      }
                      xmlHttp.open("G ET", "../ajax/", true);
                      xmlHttp.send(nu ll);
                      }
                      window.onload = ajaxFunction;
                      </script>

                      <div id="hello"></div>
                      </body>
                      </html>"""
                      return HttpResponse(ou tput)

                      def ajax(request):
                      output = """
                      <p>Hello World from Django and AJAX</p>
                      <p>Current time is: %s</p>
                      """ % str(datetime.no w())[11:19]
                      return HttpResponse(ou tput, mimetype="text/plain")

                      Note, that refresh time is in 'setTimeout('aj axFunction();', 1000);' in
                      this example it is 1 second.


                      3. edit urls.py inside YOURPROJECTNAME directory to something like this:
                      from django.conf.url s.defaults import *

                      urlpatterns = patterns('',
                      (r'^myscript/$', 'YOURPROJECTNAM E.views.myscrip t'),
                      (r'^ajax/$', 'YOURPROJECTNAM E.views.ajax'),
                      )

                      4. run 'python manage.py runserver' inside YOURPROJECTNAME directory

                      5. point your browser to 'http://127.0.0.1:8000/myscript/' and you'll see
                      the result.

                      6. Deploy your app to Apache web server



                      Hope this Django/AJAX introduction is helpfull
                      Please note that this code is extremely simplified you probably need to
                      learn more about Django and AJAX/Javascript by yourself

                      Ivan

                      Comment

                      • Lie

                        #12
                        Re: Dynamic HTML from Python Script

                        On Jun 11, 9:57 am, Aidan <awe...@gmail.c omwrote:
                        asdf wrote:
                        On Wed, 11 Jun 2008 11:20:48 +1000, Aidan wrote:
                        >
                        asdf wrote:
                        >>Well, there's a few ways you could approach it.
                        >
                        >>You could create a cgi program from your script - this is probably the
                        >>solution you're looking for.
                        >
                        >Output from the script does come up very often. There is a new output
                        >every 10 secs and it's possible that the script might be run
                        >indefinitely . Basically I want all that output displayed in a web
                        >browser
                        Well, in that case you could simply append the new output to a static
                        file every 10 seconds, or whenever there is new output.  That way, you
                        just need to refresh the static file in your browser to see updates...
                        Given what I understand of your situation, that's how I'd do it.
                        >
                        The problem with this is that browser would have to be refreshed manually
                        every 10 seconds. Unless there is a way to set this in the script itself..
                        >
                        You should be able to do that with just:
                        >
                        <meta http-equiv="refresh" content="10"/>
                        >
                        in the <headsection of your page (you can adjust the value of content
                        from 5 to however many seconds you want between refreshes).
                        That's an alternative way although many older browser doesn't support
                        it, it's probably a better way instead of using Javascript if you
                        don't care about those that are using old browser.
                        You could also look at adding some AJAX-yness to your page, and have it
                        query your script for new output every 10 seconds, and then add that
                        content to the existing page... it sounds like this behavior is what
                        you're looking for, but it's slightly harder to pull off than the method
                        mentioned above.
                        FYI: AJAX is just a very fancy name for Javascript
                        >
                        >
                        A constantly running CGI app is probably not the best idea, given
                        timeouts and other such constraints you might run into.
                        >
                        >>You could have the script run periodically and create a static html
                        >>file in the webroot... this would be acceptable, maybe preferable, if
                        >>the output from your script doesn't change frequently.
                        >
                        >

                        Comment

                        • Dave Parker

                          #13
                          Re: Dynamic HTML from Python Script

                          On Jun 11, 7:59 am, Lie <Lie.1...@gmail .comwrote:
                          You can't make the browser refresh automatically in the server side,
                          Yes you can. I don't know how to do it in Python, but here's an
                          example in Flaming Thunder of a small, fast, light compiled server
                          side CGI that delivers dynamic content every 10 seconds.

                          # Write out the HTTP headers, followed by a blank line.
                          # Make sure to write CRLF and not just LF, as per HTTP
                          # specs. Also, turn off caching using the no-cache and
                          # expires options, so that caching doesn't conflict with
                          # refreshes.

                          Set CRLF to CarriageReturn+ LineFeed.
                          Write "Refresh: 10; url=http://www.flamingthun der.com/cgi/
                          refresh.cgi",CR LF.
                          Write "Content-type: text/html",CRLF.
                          Write "Pragma: no-cache",CRLF.
                          Write "Expires: 0",CRLF.
                          Write "Cache-Control: no-cache",CRLF.
                          Write CRLF.

                          # Write out the dynamic information. In this
                          # case, we'll just write out Greenwich mean time.

                          Write GetGreenwichMea nTime.

                          For this example, the dynamic content is just Greenwich mean time.
                          You can see it in action at:



                          To create the CGI script, I used Flaming Thunder's cross compiling
                          ability to compile the script under Windows, targeting Linux:

                          ft file refresh.ft output refresh.cgi target linux32

                          I then ftp'd refresh.cgi up to the cgi directory on my server, set the
                          permissions to 700 to make it executable, and it works (without
                          needing to install any bulky, plodding interpreter).

                          On Jun 11, 7:59 am, Lie <Lie.1...@gmail .comwrote:
                          On Jun 11, 9:16 am, asdf <a...@asdf.comw rote:
                          >
                          >
                          >
                          >
                          >
                          On Wed, 11 Jun 2008 11:20:48 +1000, Aidan wrote:
                          asdf wrote:
                          >>Well, there's a few ways you could approach it.
                          >
                          >>You could create a cgi program from your script - this is probably the
                          >>solution you're looking for.
                          >
                          >Output from the script does come up very often. There is a new output
                          >every 10 secs and it's possible that the script might be run
                          >indefinitely . Basically I want all that output displayed in a web
                          >browser
                          >
                          Well, in that case you could simply append the new output to a static
                          file every 10 seconds, or whenever there is new output.  That way, you
                          just need to refresh the static file in your browser to see updates...
                          Given what I understand of your situation, that's how I'd do it.
                          >
                          The problem with this is that browser would have to be refreshed manually
                          every 10 seconds. Unless there is a way to set this in the script itself..
                          >
                          Surely you don't think you can do that without Javascript don't you?
                          You can't make the browser refresh automatically in the server side,
                          it has to be done in the client side scripting or like Opera browser
                          that have an option to make it refresh a page every few seconds.
                          >
                          >
                          >
                          A constantly running CGI app is probably not the best idea, given
                          timeouts and other such constraints you might run into.
                          >
                          >>You could have the script run periodically and create a static html
                          >>file in the webroot... this would be acceptable, maybe preferable, if
                          >>the output from your script doesn't change frequently.- Hide quoted text -
                          >
                          - Show quoted text -- Hide quoted text -
                          >
                          - Show quoted text -

                          Comment

                          • Sebastian \lunar\ Wiesner

                            #14
                            Re: Dynamic HTML from Python Script

                            Lie <Lie.1296@gmail .comat Mittwoch 11 Juni 2008 16:04:
                            FYI: AJAX is just a very fancy name for Javascript
                            AJAX is not just a "name", it's a _religion_

                            SCNR

                            --
                            Freedom is always the freedom of dissenters.
                            (Rosa Luxemburg)

                            Comment

                            • Dave Parker

                              #15
                              Re: Dynamic HTML from Python Script

                              On Jun 11, 10:43 pm, Dennis Lee Bieber <wlfr...@ix.net com.comwrote:
                              Those are not /server side/ refreshes...
                              Correct. But we weren't discussing server side refreshes. We were
                              discussing how to make the "browser refresh automatically in the
                              server side":

                              On Jun 11, 7:59 am, Lie <Lie.1...@gmail .comwrote:
                              Surely you don't think you can do that without Javascript don't you?
                              You can't make the browser refresh automatically in the server side,
                              it has to be done in the client side scripting or like Opera browser
                              that have an option to make it refresh a page every few seconds.
                              The example I posted showed a simple way to "make the browser refresh
                              automatically in the server side" by using an HTTP Refresh header
                              instead of using any Javascript or client side scripting or setting a
                              browser option to refresh the page every few seconds.


                              On Jun 11, 10:43 pm, Dennis Lee Bieber <wlfr...@ix.net com.comwrote:
                              On Wed, 11 Jun 2008 07:36:59 -0700 (PDT), Dave Parker
                              <davepar...@fla mingthunder.com declaimed the following in
                              comp.lang.pytho n:
                              >
                              Yes you can.  I don't know how to do it in Python, but here's an
                              example in Flaming Thunder of a small, fast, light compiled server
                              side CGI that delivers dynamic content every 10 seconds.
                              >
                                # Write out the HTTP headers, followed by a blank line.
                                # Make sure to write CRLF and not just LF, as per HTTP
                                # specs.  Also, turn off caching using the no-cache and
                                # expires options, so that caching doesn't conflict with
                                # refreshes.
                              >
                                Set CRLF to CarriageReturn+ LineFeed.
                                Write "Refresh: 10; url=http://www.flamingthun der.com/cgi/
                              refresh.cgi",CR LF.
                              >
                                      Those are not /server side/ refreshes... The first thing being
                              written is a command to the browser that tells the browser to reload the
                              specified page after a delay period.
                              >
                                      IOWs it is the browser doing the refresh -- which means itstarts a
                              whole new connection, receiving a page from the CGI script... Said page
                              again having a browser command to do a delayed refresh.
                              >
                                      Server side would mean that the server somehow continuously sends
                              updates WITHOUT BEING ASKED.
                              --
                                      Wulfraed        Dennis Lee Bieber               KD6MOG
                                      wlfr...@ix.netc om.com              wulfr...@bestia ria.com
                                              HTTP://wlfraed.home.netcom.com/
                                      (Bestiaria Support Staff:               web-a...@bestiaria. com)
                                              HTTP://www.bestiaria.com/

                              Comment

                              Working...