spyce
home
license
community
download
examples
resources
wishlist
contrib (@sf)
documentation
intro
lang
runtime
modules
tags
install
exits
sourceforge
statistics
freshmeat

transparent transparent transparent
Examples
[[ Spyce ]]
Python Server Pages
by Rimon Barr

examples/myPortal.spy
[[.import name=myPortal]]
[[
# this data might be pulled from a database
news = {
  'heading': 'News',
  'data': [
    ('<a href="http://www.nytimes.com">nyt</a>', 
      'today', 'sun rose'),
    ('<a href="http://www.cnn.com">cnn</a>', 
      'yesterday', 'sun set'),
    ('<a href="http://news.google.com">goo</a>', 
      'long time ago', 'let there be light!'), ] }
weather = {
  'heading': 'Weather',
  'data': [
    ('nyc', 'too cold'),
    ('seattle', 'too wet'),
    ('tucson', 'too dry'),
    ('houston', 'too humid'),
    ('chicago', 'too windy'),
    ('<a href="http://www.carrier.com/">carrier</a>', 
      'just right'), ] }
movies = {
  'heading': 'Movies',
  'data': [
    ('over-priced theatre', '15 movies'),
    ("'el cheapo", '3 movies'),
    ('home', 'blockbuster'), ] }
selection = [ news, movies, weather ]
]]

<html><body>
    Dear user [[='XYZ']], <br>
    Welcome to your portal. Here are your selected views... <br>
    [[-- a module that does a lot of output --]]
    [[ myPortal.show( selection ) ]]
</body></html>
Run this code.
(requires Spyce-enabled web server)

Supplemental files:

examples/myPortal.py
from spyceModule import spyceModule

__doc__ = '''This module takes care of presenting the portal.
Spyce lambdas are easier to use to perform the output.'''

class myPortal(spyceModule):
  def start(self):
    self.show = self._api.getModule('spylambda')(spysigPortal, spycodePortal)
    self.showView = self._api.getModule('spylambda')(spysigItem, spycodeItem)

spysigPortal = 'selection'
spycodePortal = '''
  <html><body>
    <table align=center valign=center border=1 width=100% bgcolor="#aaaaaa"><tr>
      <td width=30% >
        [[ for view in selection: {]]
          [[myPortal.showView(view['heading'], view['data'])]]
          <p>
        [[ } ]]
      </td>
      <td align=center valign=center width=50% ><b>main panel</b></td>
      <td align=center valign=center width=20% ><b>other stuff</b></td>
    </tr><table>
  </body></html>
'''

spysigItem = 'heading, data'
spycodeItem = '''
  <table cellspacing=0 border=0 bgcolor="#ffdddd" width=100% >
    <tr><td bgcolor="#bbddff" colspan=[[=max(map(len, data))]]>
      <b>[[=heading]]<b>
    </td></tr>
    [[ for row in data: { ]]
      <tr> 
        [[ for i in row: { ]]
          <td>[[=i]]</td> 
        [[ } ]]
      </tr>
    [[ } ]]
  </table>
'''

Back to List of Examples


© 2002 Rimon Barr
email: rimon AT acm DOT org
Spyce Powered SourceForge Logo [[ Spyce ]]
Python Server Pages
version 1.3.10