[Tutor] using python to write web page

ALAN GAULD alan.gauld at btinternet.com
Thu Jun 16 10:09:40 CEST 2011


________________________________
From: Michael bridges <micha_el2003 at yahoo.com>
To: ALAN GAULD <alan.gauld at btinternet.com>
Sent: Thursday, 16 June, 2011 1:07:14
Subject: Re: [Tutor] using python to write web page


> your python code includes html.

OK, Now I understand.

> i write the python only code --> web page 
> [ do not care if it is html, xhtml, java, javascript, or whatever]

But a "web page" is by definition html.
Thats what makes it a web page.

Most browsers can display plain text too but they will usually 
mess up the formatting so that it is hard to read. If you want 
to do that then just create a normal text file.

But if you want the browser4 to display it with any kind of 
formatting you must use html, thats all the browser understands.


> if i wanted to write html in the python i would just write the html
> not sure how else the restate, rewrite

The reasons for writing the html from Python are that you 
can insert dynamic content into the html, such as names 
or values from a database, or from another file:

#####################
contentStart = """"
<html><body>
<p>Here are the current members of my club:</p>
"""
contentEnd = """
</body.</html>"""

data = ["Eric Idle", "Michael Palin", "Graham Chapman"]

with open("mywebpage.htm","w") as webpage:
      webpage.write(contentStart)
      webpage.write("<ul>\n")
      for name in data:
             webpage.write("<li>%s</li>" % name)
       webpage.write("</ul>\n")
       webpage.write(contentEnd)
#######################

Now you can regenerate the web file by just adding names to data.
(and you could populate data from a text file or database etc)
     
But if you want that to display sensibly in a web browser you 
need to use html.

HTH,

Alan G.


--- On Wed, 6/15/11, ALAN GAULD <alan.gauld at btinternet.com> wrote:


>From: ALAN GAULD <alan.gauld at btinternet.com>
>Subject: Re: [Tutor] using python to write web page
>To: "Michael bridges" <micha_el2003 at yahoo.com>
>Cc: "tutor at python.org"  <tutor at python.org>
>Date: Wednesday, June 15, 2011, 4:54 PM
>
>
>
>
>From: Michael bridges <micha_el2003 at yahoo.com>
>
>To: Alan Gauld <alan.gauld at btinternet.com>
>Sent: Thursday, 16 June, 2011 0:43:35
>Subject: Re: [Tutor] using python to write web page
>
>> i do not want to write html, not in whole or in part, nor in reference.
>> just 100% python. not from a server.
>
>The code I showed was 100%  Python that created an html file 
>that could be opened in a browser without a  server.
>
>> i want to write the python only code and have python 
>> only code make the web page that can be read on 
>> any computer with a web browser.
>
>Thats exactly what my code does.
>
>> no server use, no server down load, no server at all.
>
>Thats exactly what my code does.
>
>> no python interrupter.
>
>No idea what you mean here?
>If you mean no Python interpreter how do you expect to  
>execute the python code?
>If you mean you want to read the resultant file without 
>using Python then.... thats exactly what my code does.
>
>So does my sample code meet your needs, or is there 
>some other requirement that you haven't explained yet?
>
>
>--- On Wed, 6/15/11, Alan Gauld <alan.gauld at btinternet.com> wrote:
>
>> Python can create html  files so if you only want to  create
>> an html file just do it:
>> 
>> content = """
>> <html><body>
>> <P>Hello world</p>
>> </body></html>"""
>> 
>> open("myfile.htm","w").write(contents)
>> 
>
>Alan G
> 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110616/bc444172/attachment.html>


More information about the Tutor mailing list