The PEAK Developers' Center   Diff for "PeakWebHowTo" UserPreferences
 
HelpContents Search Diffs Info Edit Subscribe XML Print View
Ignore changes in the amount of whitespace

Differences between version dated 2005-02-13 00:41:23 and 2007-03-26 19:17:30 (spanning 8 versions)

Deletions are marked like this.
Additions are marked like this.

#format rst
 
This is informal introduction to ``peak.web`` written after exercises taken to learn ``peak.web`` functionality.
This is an informal introduction to ``peak.web`` written after exercises taken to learn ``peak.web`` functionality.
 
Content producer
================
 
Suppose we have an existing application component and we want to make it accessible through the web.
Suppose we have an existing application component which we want to make accessible over the web.
 
The following example shows simple component producing an infinite series of text slides::
This shows a simple component producing an infinite series of text slides. Create the file ``slides.py`` and paste this content::
 
    #!python
    import sys
    from urllib import quote
 
    import protocols
    import peak.web.templates
    from peak.api import binding, config, naming, security, web
    from peak.running.interfaces import IRerunnable
    from peak.tools.local_server import WSGIServer

    if __name__ == "__main__":
        main()
 
Normally, DOMlet handlers are registered in section ``peak.web.werbs`` of PEAK configuration file like this::
Normally, DOMlet handlers are registered in section ``peak.web.verbs`` of PEAK configuration file like this::
 
    [peak.web.verbs]
    when-larch = pwt.negotiatorFactory(my.module.WhenLarch)

 
In this example, we have only one (root) location with two views: ``index_html``, displaying our application component, and ``larch``, publishing a static file from resource package.
 
Finally we need to have the current directory in an importable path::
 
    $ export PYTHONPATH=.
 
And now... the script to run all this::
 
    #!python

    if __name__ == "__main__":
        main()
 
There is near to zero web code in this script! If our sitemap knew where to take the component it publishes (i.e. if we used valid import specifications in ``container object`` and ``content type``), we wouldn't need our launch script at all. Sitemap may be run by itself with PEAK command line tool::
There is near to zero web code in this script! If our sitemap knew where to take the component it publishes (i.e. if we used valid import specifications in ``container object`` and ``content type``), we wouldn't need our launch script at all. To do so we first need to change our ``sitemap.xml``::
 
    <location id="root" config="resources.ini">
        <import module="slides" />
        <container object="slides.Slides()" />
        <content type="slides.ISlides">
        <view name="index_html" resource="templates/slide"/>
            <view name="larch" resource="templates/larch.jpg"/>
        </content>
    </location>
 
Notice the import of the module ``slides``, wich is our original ``slides.py``. Next we need to start the server using the ``sitemap.xml``, we use the command ``serve`` to do
it. There is a special flag ``-p <PORT>`` to specify the port we want our webserver to listen to. Finally we run peak as the following::
 
    $ peak serve ref:sitemap@file:sitemap.xml
    $ peak serve -p 8917 ref:sitemap@file:sitemap.xml
 
or, to automatically launch web browser displaying our application::
If the sitemap was located inside a Python package directory we could specify it using a ``pkgfile:`` URL in place of a ``file:`` URL. For example, if the sitemap file was in the package directory for a package named ``some.package``, we could launch the web browser displaying our application::
 
    $ peak launch ref:sitemap@pkgfile:my.module/sitemap.xml
    $ peak launch ref:sitemap@pkgfile:some.package/sitemap.xml
 
(in the later example sitemap is located in python package ``my.module``).
(The ``peak serve`` command just starts a web server. The ``peak launch`` command is similar, but also launches a web browser to display the root of the application -- a convenient development tool.)

PythonPowered
ShowText of this page
EditText of this page
FindPage by browsing, title search , text search or an index
Or try one of these actions: AttachFile, DeletePage, LikePages, LocalSiteMap, SpellCheck