Python Macro

Last modified by Vincent Massol on 2017/08/17 09:29

cogAdds ability to write scripts in Python in wiki pages
TypeJAR
Developed by

XWiki Development Team

Rating
Rate!
0 Votes
LicenseGNU Lesser General Public License 2.1
Bundled With

XWiki Enterprise, XWiki Enterprise Manager

Description

This is a shortcut for Script Macro equivalent to {{script language="python"}} to execute python scripts based on any JSR-223 Python provider like Jython.

Usage

{{python}}
print "Hello World"
{{/python}}

Parameters definition

See the Script Macro for details.

Example

{{python}}
print "Hello World"
{{/python}}

Result

Hello World

Why Scripting?

Many different wikis exist to date, each offering a plethora of features.  This example illustrates how leveraging the power of XWiki can save you time, and impress people.  Below is a link to an amusing SMBC comic:

http://smbc-comics.com/index.php?id=4071

It's entertaining, but the math is awful.  We can actually prove this with python natively in XWiki.  How cool is that?!

I have not included the following script in this page, I've left that for you to try in your own playground.  Go ahead and give it a shot.  Paste the following code into your own XWiki and see what happens.

{{python}}
square = 1
pennies = 1

while square <= 64:
print "Square: %s Pennies: %s" % (square, square)
if square == 1:
  quantity = "penny"
else:
  quantity = "pennies"
print "As of square %s you have %s %s." % (square,pennies,quantity)
print ""
 square += 1
 pennies += square
{{/python}}

External Libraries

Using the PyPI Repository

This is the recommended approach. See the PyPI Repository Connector.

Manually

External libraries can be used with XWiki by adding them to the proper classpath and importing them.

In order to determine what your classpath and pyclasspath locations are, use the following code:

{{python}}
import sys
print sys.path
{{/python}}

Once you have the proper location, you can install your libraries through your normal means (pip, dnf, apt-get, etc), and copy the library to the classpath/pyclasspath locations respectively.  After installation, a restart is required to load the new library/libraries.

Caveats

Though you can add additional libraries, there may be conflicts that you have to work through to be able to use that library with Xwiki.  An example is the 'boto' library, which conflicts due to function and class definitions that overlap.

Tags:
Created by Thomas Mortagne on 2009/09/04 15:31
    

Get Connected