Using your models outside of TurboGears

Sample Python/Ipython Session

(Status: incomplete)

The script tg_shell_setup.py should be saved in your root directory. And from this directory run the shell command:

$ ipython shell development.ini
>>> %run tg_shell_setup
>>> whos
>>> a = DBSession.query(Page).first()
>>> a.__dict__
>>> help(a)
>>> b = Page('second pagetitle', 'SecondPage of WikiWords wiki')
>>> DBSession.add(b)
>>> pages = DBSession.query(Page).all
>>> [(page.data, page.pagename, page.id) for page in pages]
>>> ...
>>> transaction.commit()
>>> quit()

You should see the output

In [3]: a=DBSession.query(Page).first()

In [4]: a.__dict__
Out[4]: 
{'_sa_instance_state': <sqlalchemy.orm.identity.IdentityManagedState object at 0x986348c>,
 'data': u'initial data',
 'id': 1,
 'pagename': u'FrontPage'}
In [5]: help(a)

In [6]: b = Page('second pagetitle', 'SecondPage of WikiWords wiki')

In [7]: DBSession.add(b)

In [8]: pages=DBSession.query(Page).all()

In [9]: [(page.data, page.pagename, page.id) for page in pages]
Out[9]: 
[(u'initial data', u'FrontPage', 1),
 ('SecondPage of WikiWords wiki', 'second pagetitle', 2)]

Python scripts

(Status: incomplete)

Download wiki20_external.py and save it in your root directory. Run as usual from the command line.

$ python wiki20_external.py

You can test your model and templates this way, and insert code from your controllers as well. The file outputs html to the screen - capture it in a file and view through your browser.

Further Exploration

Now that you have a working Wiki, there are a number of further places to explore:

  1. You can add JSON support via MochiKit.
  2. You can learn more about the Genshi templating engine.
  3. You can learn more about the SQLAlchemy ORM.

If you had any problems with this tutorial, or have ideas on how to make it better, please let us know on the mailing list! Suggestions are almost always incorporated.

Common problems:

  • paster errors:

    • have you installed TG2 in a virtual environment? If so, have you activated the virtualenv? Check your commandline prompt.
    • paster commands must be issued from the root directory.
  • python errors:

    • Insert print or pprint commands in your code and read the output on the paster serve terminal.
    • Try the code segment outside of turbogears in a debugger (see Python scripts), or run your code in either in an ipython or python shell (see Sample Python/Ipython Session).
  • Genshi/XML errors: your Genshi templates (views) must be valid XML documents ,or TG2 will raise an exception. Check your templates with an XML validator:

    • e.g. xmllint on linux or through Cygwin on Windows for example.

      $ xmllint -noout wiki20/templates/\*.html
      

      will check all your templates at once. With the -noout option, output will be produced only if files are not valid XML.

    • Use your browser to detect errors.

  • Database errors: Turn on the sqlalchemy.echo option in your development.ini file:

    [app:main]
    ...
    sqlalchemy.echo = true

    and check the SQL code emitted by SQLAlchemy in your paster serve window. Experiment with this code in an sqlite interactive session, and with SQLAlchemy in the python/ipython shell.

Table Of Contents

Previous topic

Adding a page list

Next topic

Using ToscaWidgets to Create Forms

This Page