Example usages of the spreadsheet directive

You can also try this example using the standalone command:

python rstspreadsheet.py sample.rst sample.html

Simple spreadsheet

.. spreadsheet:: Simple spreadsheet
   :eq: {2} = {0} and {1}
        {3} = int(not {2})

   === === =========== =================
    p   q    p and q     not (p and q)
   === === =========== =================
    0   0
    1   0
    0   1
    1   1
   === === =========== =================
Simple spreadsheet
p q p and q not (p and q)
0 0 0 1
1 0 0 1
0 1 0 1
1 1 1 0

Spreadsheet with python functions

.. spreadsheet:: Spreadsheet with python functions
   :eq: {2} = '{0}({1})'
        {3} = {2}
   :setup: from math import sin, cos, exp, pi
           def fac(n):
               return fac(n-1) * n if n > 1 else 1
   :format: 3:'%0.2f'

   ======== ======== ========== ======
   function argument expression result
   ======== ======== ========== ======
   fac      5
   sin      pi
   cos      pi
   exp      1
   ======== ======== ========== ======
Spreadsheet with python functions
function argument expression result
fac 5 fac(5) 120.00
sin pi sin(pi) 0.00
cos pi cos(pi) -1.00
exp 1 exp(1) 2.72

Special variables

.. spreadsheet:: Use of the special variables
   :eq: {1} = i
        {2} = last
        {3} = i == last
        {4} = sum(col.int) + 1
        {5} = cols[0].int

   == == ====== =========== ================== =============
   #  i  last   i == last   sum(col.int) + 1   cols[0].int
   == == ====== =========== ================== =============
   0
   1
   2
   3
   == == ====== =========== ================== =============
Use of the special variables
# i last i == last sum(col.int) + 1 cols[0].int
0 0 3 False 1 []
1 1 3 False 2 [0]
2 2 3 False 4 [0, 1]
3 3 3 True 8 [0, 1, 2]

Conditions

.. spreadsheet:: Use of condition
   :eq: {1:i==0} = 3
        {1:0<i<last} = col.int[-1] + 1
        {1:i==last} = sum(col.int)
        {2:i==last} = col.sum()
        {3:i==last} = col.mean()

   ======== =============== ========== ===========
   #        increment/sum   sum data   mean data
   ======== =============== ========== ===========
   0                        2.1        4.1
   1                        3.2        3.2
   2                        4.3        2.3
   sum
   ======== =============== ========== ===========
Use of condition
# increment/sum sum data mean data
0 3 2.1 4.1
1 4 3.2 3.2
2 5 4.3 2.3
sum 12 9.6 3.2

Error handling

Note

To see errors in Sphinx, you will need to set keep_warnings = True in conf.py.

Error in :setup:

.. spreadsheet::
   :eq: {1} = {0}
   :setup: this is error!

   == ==
   i  j
   == ==
   0
   1
   2
   == ==
i j
0  
1  
2  

System Message: ERROR/3 (/home/takafumi/repos/admin.tkf.bitbucket.org/rstspreadsheet/doc/sample.rst, line 2)

An error occurs while executing `:setup:`

Traceback (most recent call last):
  File "/home/takafumi/repos/admin.tkf.bitbucket.org/rstspreadsheet/rstspreadsheet.py", line 314, in wrapper
    result = func(*args, **kwds)
  File "/home/takafumi/repos/admin.tkf.bitbucket.org/rstspreadsheet/rstspreadsheet.py", line 351, in _get_scope
    exec code in scope
  File "<string>", line 1
    this is error!
                 ^
SyntaxError: unexpected EOF while parsing

Error in :eq:

.. spreadsheet::
   :eq: {1} = this is error!
        {1:this is error, too} = 1

   == ==
   i  j
   == ==
   0
   == ==
i j
0  

System Message: ERROR/3 (/home/takafumi/repos/admin.tkf.bitbucket.org/rstspreadsheet/doc/sample.rst, line 2)

Following error occurs while validating the equation of the cell in 0-th row, 1-th col: ‘this is error!’

Traceback (most recent call last):
  File "/home/takafumi/repos/admin.tkf.bitbucket.org/rstspreadsheet/rstspreadsheet.py", line 314, in wrapper
    result = func(*args, **kwds)
  File "<string>", line 1
    this is error!
                 ^
SyntaxError: unexpected EOF while parsing

System Message: ERROR/3 (/home/takafumi/repos/admin.tkf.bitbucket.org/rstspreadsheet/doc/sample.rst, line 2)

Following error occurs while validating the condition of the cell in 0-th row, 1-th col: ‘this is error, too’

Traceback (most recent call last):
  File "/home/takafumi/repos/admin.tkf.bitbucket.org/rstspreadsheet/rstspreadsheet.py", line 314, in wrapper
    result = func(*args, **kwds)
  File "<string>", line 1, in <module>
NameError: name 'this' is not defined

Advanced

twitter

.. spreadsheet:: pipy's latest tweets
   :eq: {1} = tweets[i]['name']
        {2} = tweets[i]['version']
        {3} = tweets[i]['desc']
   :setup: import twitter
           def parse(s):
               remain = s.text
               (name, remain) = remain.split(' ', 1)
               (version, remain) = remain.split(':', 1)
               (desc, remain) = remain.split('http://', 1)
               return dict(name=name, version=version, desc=desc)
           api = twitter.Api()
           statuses = api.GetUserTimeline('pypi')
           tweets = map(parse, statuses)

   === ======== ======== ==============================================
   #   Name     Version  Description
   === ======== ======== ==============================================
   1
   2
   3
   4
   5
   6
   7
   8
   9
   10
   === ======== ======== ==============================================
pipy’s latest tweets
# Name Version Description
1 rstspreadsheet 0.1.2 Add the `spreadsheet` directive to reStructuredText for Docutils and Sphinx
2 medialog.foldersubskins 0.3 Makes it possible to have different layout on different parts of a plone site.
3 Products.Gloworm 1.1 A Firebug-like inspection tool for Plone
4 TaskCoach 1.2.20 Your friendly task manager
5 ostrich 0.3.6 Python port of the Scala Ostrich library
6 bluetrain 0.9.4 Django CMS
7 bluetrain 0.9.3.1 Django CMS
8 armstrong 11.06.0.beta.2 Armstrong is an open-source publishing system designed for news organizations that giv...
9 ManifestDestiny 0.5.1 Universal manifests for Mozilla test harnesses
10 pydub 0.5.2 Manipulate audio with an simple and easy high level interface

Comments

blog comments powered by Disqus