Review of Grok 1.0 Web Development

April 12, 2010

It took me a while to finish reading the book but here we go.

I wasn’t disappointed by the book and absolutely sure that it delivers on its promise to show that Grok can be an excellent fit for many kinds of Web development projects.

I think I’ll have to finish my Grok based blog now :)

Grok is largely based on Zope Tookit and that’s a very powerful set of libraries developed over many years that provides support for forms, full text search, authentication, i18n, security, components, persistence, testing, and many more.

Main distinguishing Grok concepts emphasized by the book:

  • component architecture
  • object database (ZODB)
  • object publishing and traversal
  • convention over configuration and do not repeat yourself (DRY) principle

The book introduces a simple “to-do” application to the readers. Gradually the application grows with new features like storing data in object database, forms, full text catalog search, security, component architecture.

There is a separate chapter devoted to integration with relational databases. This is especially important topic for Grok because traditionally Zope and Grok were tightly bound to ZODB.

All in all it’s been a pleasant read and I think it’s a great introduction to Grok web framework.

P.S. I may sound very optimistic regarding Grok and Zope, but I can’t do anything about it – I have a soft spot for Grok, it’s an excellent piece of software :)

Enjoy it!


Preview of Grok 1.0 Web Development by Carlos de la Guardia

February 26, 2010

I’ve been asked by PACKT publishing to review a new book  Grok 1.0 Web Development.

I gladly accepted their proposal. For those interested – yes, they sent me a free PDF version of the book, but I would buy the book anyway because it is about the project I love and associate myself with even though I’ve not been actively participating in it recently.

Personally I think that the release of this book is a major event for the whole Grok community. Having comprehensive documentation for people just starting with a smashing Web framework Grok is of utter importance.

What’s interesting is that I’ve been thinking about writing a book on Grok myself, but Carlos de la Guardia beat me to the punch and after skimming through the book I’m glad he did :)

What I liked instantaneously about the book is the presence of a chapter on integration with relational databases, the topic that in the Zope world was kind of obscure in the past.

When I finish reading the book I’ll post a full review so stay tuned and meanwhile you can take a sneak peak at the book by reading a freely available Chapter No 5: Forms



IPython profile for Grok

March 30, 2009

To use IPython shell from Grok based project is easy, just add ipython to eggs directive in your buildout.cfg

My part with ipython added looks like this:

[app]
recipe = zc.recipe.egg
eggs = grok-awesome
       ipython
       Paste
       PasteScript
       PasteDeploy
interpreter = python-console

But I wanted to be able to inspect Grok instance and use path TAB auto-completion to navigate ZODB hierarchy.

Inspired by existing IPython profile fo Zope2 I whipped out basic implementation for Grok.

It’s available under grok-ipython. Now all IPython power can be used when inspecting objects in Grok instance:

(mygrok)[alienoid@capricorn grok-awesome]$ bin/ipython -p grok
IPython shell for Grok.

Bound object names:
-------------------
  root
  ctx

Bound command names:
--------------------
  cdg / ;cdg
  lsg / ;lsg
  pwdg
  sync
  commit

Grok IPython 0.9.1   Py 2.5.2

In [1]: root
Out[1]: <zope.app.folder.folder.Folder object at 0x9003e6c>

In [2]: ctx
Out[2]: <zope.app.folder.folder.Folder object at 0x9003e6c>

In [3]: ;cdg blog
------> cdg("blog")
Out[3]: u'/blog'

In [4]: ctx
Out[4]: <grokawesome.blog.Blog object at 0x81d2d6c>

In [5]: ;cdg 
my-first-post  second-post    

In [5]: ;cdg my-first-post
------> cdg("my-first-post")
Out[5]: u'/blog/my-first-post'

In [6]: ctx
Out[6]: <grokawesome.entry.BlogEntry object at 0xb7d556ac>

In [7]: pwdg
Out[7]: u'/blog/my-first-post'

In [8]: ctx?
Type:           BlogEntry
Base Class:     <class 'grokawesome.entry.BlogEntry'>
String Form:    <grokawesome.entry.BlogEntry object at 0xb7d556ac>
Namespace:      Interactive
Length:         0
File:           /home/alienoid/mygrok/grok-awesome/src/grokawesome/entry.py
Docstring:
    <no docstring>


In [9]: ctx??
Type:             BlogEntry
Base Class:       <class 'grokawesome.entry.BlogEntry'>
String Form:   <grokawesome.entry.BlogEntry object at 0xb7d556ac>
Namespace:        Interactive
Length:           0
File:             /home/alienoid/mygrok/grok-awesome/src/grokawesome/entry.py
Source:
class BlogEntry(grok.Container):
    grok.implements(IBlogEntry)

    portal_type = 'blogentry'
    created = property.DCProperty('created')
    modified = property.DCProperty('modified')

    def __init__(self, title, content,
                 summary=u'',
                 categories=None,
                 allow_comments=False):
        super(BlogEntry, self).__init__()
        self.title = title
        self.content = content
        self.summary = '' if summary is None else summary
        self.categories = [] if categories is None else categories
        self.allow_comments = allow_comments

TinyMCE widget in Grok

March 24, 2009

I have added TinyMCE widget to grok-awesome for editing html and it was easy as pie.

If you want TinyMCE in your Grok based project it can be achieved with following steps:

1. Add dependencies to setup.py into install_requires section:

                        'hurry.tinymce',
                        'hurry.zopetinymce',

hurry.tinymce allows you to include TinyMCE into your project without additional burden, you don’t need to do anything. Well, almost – just make sure to call:
from hurry.tinymce import tinymce
tinymce.need()

to trigger inclusion of TinyMCE in the web page.

2. Create custom widget which calls tinymce.need() as noted above and initializes TinyMCE

from zope.app.form.browser import TextAreaWidget
from hurry.tinymce import tinymce

template = """%(widget_html)s
<script type="text/javascript">
  tinyMCE.init({
    mode : "exact",
    elements: "%(elements)s",
    theme: "advanced"
  });
</script>"""

class TinyMCEWidget(TextAreaWidget):
    """Widget to edit html using TinyMCE WYSIWYG editor."""

    def __call__(self, *args, **kw):
        widget_html = super(TinyMCEWidget, self).__call__(*args, **kw)
        tinymce.need()
        return template % {'widget_html': widget_html,
                           'elements': self.name,
                           }

3. In your add/edit form use your custom widget for the field you want and off you go:

form_fields['content'].custom_widget = TinyMCEWidget

Refresh Firefox page from Emacs

March 16, 2009

I often need to refresh page in Firefox when I edit page template or javascript file. This is especially true when working on grok-awesome.

Though I use tiling window manager StumpWM and it’s very easy to switch to running Firefox with keyboard shortcut and make refresh, it’s even easier not to leave focus from Emacs window and just send command to Firefox to refresh itself.
For that I use:

  • MozRepl firefox plugin to allow connect to Firefox’s REPL. After installation in Tools -> MozRepl menu I chose ‘Activate on startup’ for convenience
  • MozRepl Emacs integration (If you use nXhtml package in Emacs you already have that integration and can skip this part)

Command to refresh Firefox is BrowserReload();

Final touch is small anonymous function to send that command to mozrepl, keybinding to C-x p and we’re all set:

(global-set-key (kbd "C-x p")
                (lambda ()
                  (interactive)
                  (comint-send-string (inferior-moz-process)
                                      "BrowserReload();")))

Now anytime just press C-x p and refresh command will be sent to Firefox starting along the way mozrepl session in your Emacs if it’s not been already started.


Grok Awesome

March 12, 2009

I’ve started developing blog software for “A Smashing Web Framework” Grok.

The reasons behind that:

  • There is no full-featured blog software for Grok. There is a simple version Grokstar, but as for me it doesn’t fit the bill.
  • I created simple blog for Zope3 several years ago and let it languish. Now it’s time to revive it taking into account how Grok simplifies development with Zope3 technologies.
  • I’m not completely happy with WordPress. For one thing, I’d like to have easy-to-use REST API so I can use it in my Emacs to make posts.
  • I simply love Grok. You should try it and you’ll love it too.

My fledgling project lives on GitHub and it’s called grok-awesome :)