How import Python packages to Mako

914 views
Skip to first unread message

Mario Moura

unread,
May 17, 2010, 10:22:54 AM5/17/10
to pylons-...@googlegroups.com
Hi Folks

I want use html 1.11 from Package Index inside mako. (and anothers packages)

Is it possible? how?

http://pypi.python.org/pypi/html

Regards

Mario
macm

--
You received this message because you are subscribed to the Google Groups "pylons-discuss" group.
To post to this group, send email to pylons-...@googlegroups.com.
To unsubscribe from this group, send email to pylons-discus...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/pylons-discuss?hl=en.

Eryx Lee

unread,
May 17, 2010, 11:48:52 PM5/17/10
to pylons-...@googlegroups.com
import html in Mako:

<%!
    from html import HTML
    h = HTML()
%>

a test ${str(h.p('hello world!\n'))|n}

Mario Moura

unread,
May 24, 2010, 5:33:36 PM5/24/10
to pylons-...@googlegroups.com
Hi All

I not talking about import
/templates/modules/something.py
or
/templates/modules/something.html

I am talking about pypi packages.

Could you give a easy example like:

easy_install jsmin

## /templates/myminimizer.mako
<%namespace name="hw" file="/jsmin/__init__.py" import="*" /> NOT WORKING
<%namespace name="hw" module="jsmin" /> NOT WORKING
<%
import jsmin ## NOT WORKING
%>

## toMinimizer is a javascript code
toMinimizer = '
    function decodeit_ve() {
            inpdata=window.document.blcoder.blinptext.value;
            passwd=window.document.blcoder.blpassword.value;
            window.document.blcoder.blouttext.value="Working...";
        if ( window.document.blcoder.bltype.options[0].selected )
            decodetext(2);
        if ( window.document.blcoder.bltype.options[1].selected )
            decodetext(0);
        // if ( window.document.blcoder.bltype.options[2].selected )
        //    decodetext(1);
            window.document.blcoder.blouttext.value=outdata;
        inpdata="";
        outdata="";
        passwd="";
        }
        '

${hw.jsmin(toMinimizer)} NOT WORKING

I tried a lot!

And I read http://www.makotemplates.org/docs/namespaces.html

from mako.runtime import supports_caller
@supports_caller

Regards

Mario
macm

2010/5/18 Eryx Lee <eryx.l...@gmail.com>

Didip Kerabat

unread,
May 24, 2010, 8:36:51 PM5/24/10
to pylons-...@googlegroups.com
Usually I do that by importing the module into helpers.py

Then you can just call h.jsmin

- Didip -

Mike Orr

unread,
May 24, 2010, 10:00:03 PM5/24/10
to pylons-...@googlegroups.com
Well, I don't understand what the original problem is. If he can't do
"<% import package %>" within a template, he probably can't import it
outside the template either. That suggests it was installed
incorrectly or the Python path or virtualenv is screwed up.

I think you can use <%namespace ...> to import pure Python modules,
but the functions have to be written to use Mako's special context
argument.
--
Mike Orr <slugg...@gmail.com>

Eryx Lee

unread,
May 24, 2010, 10:25:15 PM5/24/10
to pylons-...@googlegroups.com
Mako compiles template into .py file finally, you can get it in data/template directory. So if your package can be imported correctly in your python file, you can use it in Mako too.

In above example, I first install html package using easy_install html, then import it in Mako template directly. This example works correctly in my pylons env.
Reply all
Reply to author
Forward
0 new messages