pythonmoinmoin

Is there a way to incorporate python code into moinmoin pages?


I was wondering whether there is a trick to (easily) incorporate some python code into a moinmoin page, perhaps by adding some action. The idea is that something like

<<<
for j in [1,3,5]:
   print(i)
>>>

is displayed on the page as

1
3
5

Quick and dirty is ok, safety is not a concern, I would like to have this for a stand-alone, "desktop-mode" installation.


Solution

  • In your moinmoin instance go to

    data/macro
    

    Make sure that there's an __init__.py with at least the following content:

    from MoinMoin.util import pysupport
    
    modules = pysupport.getPackageModules(__file__)
    

    Then create your macro there:

    touch PyShell.py
    

    Use this as a template:

    import StringIO
    import sys
    
    def macro_PyShell(macro, code):
      code_out = StringIO.StringIO()
      sys.stdout = code_out
      exec code.strip("'").strip('"')
      sys.stdout = sys.__stdout__
      return macro.request.formatter.text(code_out.getvalue())
    

    In your wiki-pages you should now be able to call that macro with:

    <<PyShell("for i in [1,2,3]:\n  print i")>>