Commit eb74569d817d9c581127a0410fa9aa843f6fd408

Authored by Jürgen Knödlseder
1 parent 528c686a

Add initial example script.

This script creates a simple HTML document using GammaLib's xml module.
examples/python/xml_html_create.py 0 → 100755
  1 +#! /usr/bin/env python
  2 +# ==========================================================================
  3 +# Scope
  4 +#
  5 +# This script creates a simple HTML Web page using GammaLib's xml module
  6 +#
  7 +# Usage
  8 +# ./xml_html_create.py
  9 +#
  10 +# -------------------------------------------------------------------------
  11 +#
  12 +# Copyright (C) 2013 Juergen Knoedlseder
  13 +#
  14 +# This program is free software: you can redistribute it and/or modify
  15 +# it under the terms of the GNU General Public License as published by
  16 +# the Free Software Foundation, either version 3 of the License, or
  17 +# (at your option) any later version.
  18 +#
  19 +# This program is distributed in the hope that it will be useful,
  20 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  21 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22 +# GNU General Public License for more details.
  23 +#
  24 +# You should have received a copy of the GNU General Public License
  25 +# along with this program. If not, see <http://www.gnu.org/licenses/>.
  26 +#
  27 +# ==========================================================================
  28 +from gammalib import *
  29 +
  30 +
  31 +# ==================== #
  32 +# Create HTML document #
  33 +# ==================== #
  34 +def create_html():
  35 + """
  36 + Illustrates how GammaLib can be used to create HTML documents.
  37 +
  38 + The HTML document is composed of a main element with name 'html'.
  39 + This elements has two childs: a 'head' child for the header and a
  40 + 'body' child for the page information.
  41 +
  42 + The header contains metadata in the 'meta' element and a page
  43 + title defined by the 'title' element.
  44 +
  45 + The body contains arbitrary text. In this example there are two
  46 + lines of text with different font formatting and a GammaLib icon
  47 + that is placed in the top-right corner of the page.
  48 + """
  49 + # Allocate XML document
  50 + xml = GXml()
  51 +
  52 + # Creates HTML base
  53 + html = xml.append('html')
  54 +
  55 + # Writes header
  56 + header = html.append('head')
  57 + meta = header.append('meta content="text/html; charset=ISO-8859-1" '
  58 + 'http-equiv="content-type"')
  59 + title = header.append('title')
  60 + text = title.append(GXmlText('GammaLib'))
  61 +
  62 + # Writes body
  63 + body = html.append('body')
  64 + image = body.append('img style="float: right;" alt="GammaLib" '
  65 + 'src="http://a.fsdn.com/allura/p/gammalib/icon"')
  66 + big = image.append('big')
  67 + bigger = big.append('big')
  68 + span = bigger.append('span style="font-family: Arial; font-weight: bold;"')
  69 + text = span.append(GXmlText('Gammalib'))
  70 + text = image.append(GXmlText('<br>'))
  71 + span = image.append('span style="font-family: Arial;"')
  72 + text = span.append(GXmlText('A versatile toolbox for the scientific analysis '
  73 + 'of astronomical gamma-ray data'))
  74 +
  75 + # Return XML document
  76 + return xml
  77 +
  78 +
  79 +# ================= #
  80 +# Show XML document #
  81 +# ================= #
  82 +def show_xml(xml):
  83 + """
  84 + Show XML document on the screen.
  85 + """
  86 + # Allocate string URL
  87 + url = GUrlString()
  88 +
  89 + # Write XML document in URL
  90 + xml.write(url)
  91 +
  92 + # Print URL buffer
  93 + print(url.string())
  94 +
  95 + # Return
  96 + return
  97 +
  98 +
  99 +# ======================== #
  100 +# Main routine entry point #
  101 +# ======================== #
  102 +if __name__ == '__main__':
  103 + """
  104 + This script creates a simple HTML Web page using GammaLib's xml module.
  105 + """
  106 + # Create HTML XML document
  107 + xml = create_html()
  108 +
  109 + # Show XML document
  110 + show_xml(xml)
  111 +
  112 + # Save HTML document
  113 + xml.save("gammalib.html")
... ...