Ma première contribution au monde de l'Open Source :D
Bon d'accord, c'est une maigre contribution...
EXPLICATIONS / PRISE EN MAIN
HTMLOutput est une classe très simple (malgré les commentaires en Anglais) qui permet de générer une page XHTML à partir de 2 éléments seulement:
- un titre (+ un titre de page)
- un contenu
Dans l'ordre:
1) appel de la classe
2) ajout d'un ou plusieurs éléments de titre
3) ajout des éléments de contenu
4) récupération du résultat pour affichage
Pour un exemple, voir l'archive ZIP.
PERSONNALISATION
La balise de titre (pour ceux qui connaissent un peu l'HTML) est définie en appelant la classe (h1, h2, ... h6, ou même div, p, ou quoique ce soit d'autre).
Pour utiliser une feuille de styles CSS, il faut modifier la ligne 70:
<link rel="stylesheet" type="text/css" href="stylesheet.css" />
Pour supprimer le tableau d'en-tête pouvant contenir des infos additionnelles sur la page, supprimez les lignes 79 à 83. Si vous voulez les conserver, éditez-les!
UTILISATION
Je l'avais développé pour utiliser Python (version 2.5) en CGI sur un serveur web, et je me dits que ça peut certainement servir à d'autres pour créer des pages web simples sans se prendre la tête.
Un exemple d'utilisation est donné dans l'archive ZIP.
Source / Exemple :
#! /usr/bin/python -u
###################################################################
# created by Justin MASSIOT, august 2008 #
###################################################################
"""
To insert a stylesheet (CSS, Cascading Style Sheet), you have to edit the line 70:
<link rel="stylesheet" type="text/css" href="stylesheet.css" />
To delete the author headers, delete lines 79 to 83; otherwise edit them!
"""
__doc__ = """
This file is useful for Python scripts executed as CGI scripts (on a webserver for example).
It offers the possibility to simply create an XHTML output to display in a webbrowser.
Usage:
1) begin by calling the HTMLOutput class => HTMLOutput()
2) you can add a title calling add_title(title) (can be called several times)
3) you can add content to the page calling add_content(content) (can be called several times)
4) you MUST call footer() to finish HTML standard tags, but over all to output the final result (otherwise nothing will be printed!)
The page always starts with those two essential lines:
print "Content-type: text/html"
print
=> it specifies that the page produces an HTML output
"""
import string, os.path
############################################################ 60
# class for CGI execution of Python scripts,
# which deals with the correct format for HTML output
############################################################ 60
class HTMLOutput:
def __init__(self, HTMLpageTitle, HTMLtitleTag, HTMLcontent=''):
# this file returns HTML content, so we specify it!
self.HTMLheader = "Content-type: text/html\n"
self.innerHTMLheader = "<!-- This program is running in HTML/CGI-Python mode. -->\n"
# HTML page constructor
self.titleTag = HTMLtitleTag
self.innerHTMLheader += self.header(HTMLpageTitle)
self.innerHTMLtitle = ""
if HTMLcontent != "": self.innerHTMLpageContent = HTMLcontent
else: self.innerHTMLpageContent = ""
def header(self, title):
# HTTP head and HTML filehead
HTMLheader = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"\n'
HTMLheader += '\t\t"
http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">\n\n'
HTMLheader += '<html xmlns="
http://www.w3.org/1999/xhtml" xml:lang="fr" lang="fr">\n\n'
# HTML <head> tag
HTMLheader += '<!-- file head -->\n'
HTMLheader += '<head>\n'
HTMLheader += '\t<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />\n'
HTMLheader += '\t<link rel="stylesheet" type="text/css" href="stylesheet.css" />\n'
HTMLheader += '\t<title>'+ title +'</title>\n'
HTMLheader += '</head>\n'
HTMLheader += '<!-- end file head -->\n\n\n'
# HTML <body> tag begins here
HTMLheader += '<!-- HTML visible content -->\n'
HTMLheader += '<body>\n'
# creator signature
HTMLheader += '<table style="width: 100%;">\n'
HTMLheader += '<tr><td style="text-align: left;">Links or pieces of information</td>\n'
HTMLheader += '<td style="text-align: right;">Author name here</td></tr>\n'
HTMLheader += '</table>\n\n'
return HTMLheader
def footer(self):
# add HTML title end
if len(self.innerHTMLtitle) != 0: # if title exists, tag must be closed
self.innerHTMLtitle += '\n</'+ self.titleTag +'>\n'
#self.innerHTMLtitle += '\n<p> </p>'
# add HTML content
if len(self.innerHTMLpageContent) != 0:
self.innerHTMLpageContent = "\n" + self.innerHTMLpageContent # new line
# HTML page end
HTMLfooter = '\n</body>\n'
HTMLfooter += '<!-- end HTML visible content -->\n\n'
HTMLfooter += '</html>\n'
# print HTTP headers (impossible to return for technical reasons)
print self.HTMLheader
print # end of HTTP headers
# return the whole page : HTTP headers, HTML headers, title, content, and footer
return self.innerHTMLheader + self.innerHTMLtitle + self.innerHTMLpageContent + HTMLfooter
def add_title(self, HTMLtitle):
# add a title (or several) to the page
innerHTMLopenTitle = '<'+ self.titleTag +' style="text-align: center;">'
if string.find(self.innerHTMLtitle, innerHTMLopenTitle) == -1: # the first time a title is added
self.innerHTMLtitle = innerHTMLopenTitle + "\n\t" + HTMLtitle
else: # a title already exists
self.innerHTMLtitle += "\n\t" + HTMLtitle
def add_content(self, HTMLcontent): # add content to the HTML visible page
self.innerHTMLpageContent += HTMLcontent + "\n"
############################################################ 60
Conclusion :
Une classe pour créer rapidement des pages HTML à la volée à partir de vos scripts Python.
Vous n'êtes pas encore membre ?
inscrivez-vous, c'est gratuit et ça prend moins d'une minute !
Les membres obtiennent plus de réponses que les utilisateurs anonymes.
Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.
Le fait d'être membre vous permet d'avoir des options supplémentaires.