Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 879
mitchus Avatar asked Feb 25 '12 11:02

mitchus


2 Answers

Another approach is to add a parser.

Below is a very quick and dirty adaptation of Moinmoin's Hello World parser.

import StringIO
import sys

class Parser():        

    def __init__(self, raw, request, **kw):
        self.raw = raw
        self.request = request
        self.kw=kw

    def format(self, formatter):       
        # Execute the code
        code_out = StringIO.StringIO()
        sys.stdout = code_out
        exec self.raw
        sys.stdout = sys.__stdout__
        out_lines = code_out.getvalue().split("\n")

        # Print the output
        for out_line in out_lines:
            self.request.write(formatter.rawHTML(out_line+"<br>"))

If it is saved for example as path/to/MoinMoin/parser/interpret_python.py, then you can enter into a page

{{{#!interpret_python
for j in [1,3,5]:
   print(j)
}}}

and it will produce the text

1
3
5

when viewing the page. Can be made safe(r) by confining the execution to a sandbox.

like image 100
mitchus Avatar answered Nov 11 '22 00:11

mitchus


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")>>
like image 41
hoffmaje Avatar answered Nov 11 '22 02:11

hoffmaje