Ir al contenido principal

Ralsina.Me — El sitio web de Roberto Alsina

PyQt by example (Session 2)

I am fi­nal­ly pub­lish­ing my Lati­noWare 2008 tu­to­ri­al, in re­vised and ex­pand­ed for­m. It will prob­a­bly be a 10-­part se­ries, and here is ses­sion 2.

See al­so ses­sion 1.

Using assistant from PyQt

The uRSSus doc is slow­ly grow­ing, so I hooked as­sis­tant to the UI. Not dif­fi­cult, but I have not seen it else­where.

Here's how:

As­sume the "Hand­book ac­tion" is called ac­tion_Hand­book. Set win­dow.as­sis­tant to None in __init__.

def on_action_Handbook_triggered(self, i=None):
    if i==None: return

    if not self.assistant or \
       not self.assistant.poll()==None:

        helpcoll=os.path.join(os.path.abspath(os.path.dirname(__file__)),\
                              'help',\
                              'out',\
                              'collection.qhc')
        cmd="assistant -enableRemoteControl -collectionFile %s"%helpcoll
        self.assistant=subprocess.Popen(cmd,
                                        shell=True,
                                        stdin=subprocess.PIPE)
    self.assistant.stdin.write("SetSource qthelp://urssus/doc/handbook.html\n")

And that's it. Now I ned to fig­ure out con­text help.

Rawdog is flexible: using Mako templates

Wh­y? It's ba­si­ca­lly un­do­cu­men­te­d, it looks ugly and it does­n't su­pport tem­pla­te inhe­ri­tan­ce, whi­ch in this ca­se is ve­ry use­fu­l, be­cau­se I am ac­tua­lly doing two ve­ry si­mi­lar pla­ne­ts: 1 2.

So, sin­ce I saw a plu­gin to use Ve­llum tem­pla­tes, I thou­gh­t, why not use my fa­vou­ri­te tem­pla­ting li­bra­ry (Mako) ins­tea­d?

It turns out to be ve­ry ea­sy to do!

Just put this in your plu­gins fol­der and start using Mako tem­pla­tes (ye­s, the pla­ne­t's con­fig and stu­ff is in gi­thub. Why no­t?).

# -*- coding: utf-8 -*-
import rawdoglib.plugins

from mako.template import Template
from mako.lookup import TemplateLookup
from mako import exceptions

class MakoPlugin:
    def __init__(self):
        self.lookup = TemplateLookup(directories=['.'],\
                              output_encoding='utf-8',\
                              encoding_errors='replace')

    def fill_template(self,template, bits, result):
        self.lookup.put_string('actual',template)
        t=self.lookup.get_template('actual')
        try:
            r=t.render_unicode(**bits)
        except:
            r=exceptions.html_error_template().render()
        result.value=r
        return False

p = MakoPlugin()
rawdoglib.plugins.attach_hook("fill_template",\
                              p.fill_template)

Yu­p, 20 li­nes of co­de!


Contents © 2000-2022 Roberto Alsina