Warning
This documentation is under construction. See the Documentation Status page in wiki for the current status and timeline.
This chapter narrates creating a web application project using BlueBream. If you complete this chapter, you should be able to:
Before proceeding, here is an overview of the sections.
This document assume that you have already installed Python 2.6 and setuptools. As an alternative to setuptools, you can install distribute. If setuptools or distribute is installed you will get an easy_install command which you can use to install bluebream distribution.
You can also install BlueBream inside a Python environment created using virtualenv. Although, virtualenv may not be required when you are working on the application itself. Because, Buildout support will available in the application created. Buildout is the recommended approach to create repeatable, isolated working environments. Buildout is a declarative, configuration driven build system created by Jim Fulton.
It is recommended to use a custom built Python for working with BlueBream. You will be required to install a C compiler (gcc) in your system. Internet access to PyPI is required to perform installation of bluebream distribution. Later, to build the application using Buildout and to bootstrap the buildout itself, you need internet access. But for deployment, you can avoid the internet access using zc.sourcerelease package.
If you have installed setuptools or distribute an easy_install command will be available. Then, you can install BlueBream using easy_install command like this:
$ easy_install bluebream
As mentioned earlier, Internet access to PyPI is required to perform installation of bluebream distribution. If you use any proxy, make sure it works. The easy_install will look for the environment variable named http_proxy in GNU/Linux platforms. You can set it like this:
$ set http_proxy="http://username:password@PROXY-IP-ADDRESS:PORT"
Apart from bluebream distribution, easy_install will download and install its dependencies. The dependencies are:
The installation of bluebream project template package is a one time process. Once the project package is ready, you don’t require the bluebream project template package anymore. Because, the project package you are going to create will be self-bootstrappable.
The bluebream distribution provides project templates based on PasteScript template. Once BlueBream is installed, run paster command to create the project directory structure. The create sub-command provided by paster will show a wizard to create the project directory structure.
$ paster create -t bluebream
This will bring a wizard asking details about your new project. If you provide package name, namespace package name and version number, you will get a working application which can be modified further. The project name will be used as the name of egg. You can also change the values provided later.
Here is a screenshot of sample project creation:
The project name can be give given as a command line argument:
$ paster create -t bluebream sampleproject
The name of namespace package also can be given from the command line:
$ paster create -t bluebream sampleproject namespace_package=mycompany
If you provide an option from the command line, it will not be prompted by the wizard. The other variables are give below, you may be give the values from command line, if required:
If you are in a hurry, you can simply press Enter/Return key and change the values later. But it would be a good idea, if you provide a good name for your project.
Note
Alternate Project Templates
Alternate project templates will be available from 1.0b1 release onwards, and it is documented in the wiki.
As mentioned earlier, the generated package is bundled with Buildout configuration (buildout.cfg) and Buildout bootstrap script (bootstrap.py). First you need to bootstrap the buildout itself:
$ cd sampleproject
$ python2.6 bootstrap.py
The bootstrap script will install zc.buildout and setuptools package. Also, it will create the basic directory structure.
Here is a screenshot of bootstrapping the buildout:
Next step is building the application. To build the application, run the buildout:
$ ./bin/buildout
Here is a screenshot of building the application:
The buildout script will download all dependencies and setup the environment to run your application. The next section will show the basic usage.
The most common thing you need while developing application is running the server. BlueBream use paster command provided by PasteScript to run the WSGI server. To run the server, you can pass the PasteDeploy configuration file as the argument to serve sub-command as given here:
$ ./bin/paster serve debug.ini
After running the server, you can access the site from browser here: http://localhost:8080/ . The port number (8080) can be changed from the PasteDeploy configuration file (debug.ini).
When you open the browser, it will look like as given in this screenshot:
The second most common thing must be running the test cases. BlueBream by create a testrunner using the zc.recipe.testrunner Buildout recipe. You can see a test command inside the bin directory. To run test cases, you can run this command:
$ ./bin/test
Sometimes you may want to get the debug shell. BlueBream provides a Python prompt with your application object. You can invoke the debug shell like this:
$ ./bin/paster shell debug.ini
More about the test runner and debug shell will be exaplained in the BlueBream Manual.
The default directory structure created by the bluebream paster project template will look like this:
myproject/
|-- bootstrap.py
|-- buildout.cfg
|-- debug.ini
|-- deploy.ini
|-- etc/
| |-- site.zcml
| `-- zope.conf
|-- setup.py
|-- src/
| |-- mynamespace.egg-info/
| `-- mynamespace/
| |-- __init__.py
| `-- main/
| |-- application.zcml
| |-- app.py
| |-- configure.zcml
| |-- debug.py
| |-- ftesting.zcml
| |-- __init__.py
| |-- interfaces.py
| |-- README.txt
| |-- securitypolicy.zcml
| |-- startup.py
| |-- tests.py
| |-- views.py
| `-- static/
| |-- logo.png
| `-- style.css
|-- templates/
| `-- zope_conf.in
|-- var/
`-- versions.cfg
The name of top-level directory will be always what you gave as project name in the wizard. The name of egg also will be same as that of package name by default. But if you want, you can change it to something else from setup.py. Here are the details about other files inside the project.
The next few sections will explain how to create hello world applications.
You can watch the video creating hello world application here:
To create a web page which displays Hello World!, you need to create a view class and register it using browser:page ZCML directive. In BlueBream, this is called as Browser Page. Sometimes more generic term, View is used instead of Browser Page which can be used to refer to HTTP, XMLRPC, REST and other views. By default, the default page which you are getting when you access: http://localhost:8080 is a page registered like this. You can see the registration inside configure.zcml, the name of view will be index. You can access the default page by explicitly mentioning the page name in the URL like this: http://localhost:8080/@@index. You can refer the Default view for objects HOWTO for more details about how the default view for a container object is working.
First you need to create a Python file named myhello.py at src/mynamespace/main/myhello.py:
$ touch src/mynamespace/main/myhello.py
You can define your browser page inside this module. All browser pages should implement zope.publisher.interfaces.browser.IBrowserView interface. An easy way to do this would be to inherit from zope.publisher.browser.BrowserView which is already implementing the IBrowserView interface.
The content of this file could be like this:
from zope.publisher.browser import BrowserView
class HelloView(BrowserView):
def __call__(self):
return "Hello World!"
Now you can register this page for a particular interface. So that it will be available as a browser page for any object which implement that particular interface. Now you can register this for the root folder, which is implementing zope.site.interfaces.IRootFolder interface. So, the registration will be like this:
<browser:page
for="zope.site.interfaces.IRootFolder"
name="hello"
permission="zope.Public"
class=".myhello.HelloView"
/>
Since you are using the browser XML namespace, you need to advertise it in the configure directive:
<configure
xmlns="http://namespaces.zope.org/zope"
xmlns:browser="http://namespaces.zope.org/browser">
You can add this configuration to: src/mynamespace/main/configure.zcml. Now you can access the view by visiting this URL: http://localhost:8080/@@hello
Note
The @@ symbol for view
@@ is a shortcut for ++view++. (Mnemonically, it kinda looks like a pair of goggle-eyes)
To specify that you want to traverse to a view named bar of content object foo, you could (compactly) say .../foo/@@bar instead of .../foo/++view++bar.
Note that even the @@ is not necessary if container foo has no element named bar - it only serves to disambiguate between views of an object and things contained within the object.
In this example, you will create a hello world using a page template.
First you need to create a page template file inside your package. You can save it as src/mynamespace/main/helloworld.pt, with the following content:
<html>
<head>
<title>Hello World!</title>
</head>
<body>
<div>
Hello World!
</div>
</body>
</html>
Update configure.zcml to add this new page registration.
<browser:page
name="hello2"
for="*"
template="helloworld.pt"
permission="zope.Public" />
This declaration means: there is a web page called hello2, available for any content, rendered by the template helloworld.pt, and this page is public. This kind of XML configuration is very common in BlueBream and you will need it for every page or component.
In the above example, instead of using zope.site.interfaces.IRootFolder interface, * is used. So, this view will be available for all objects.
Restart your application, then visit the following URL: http://127.0.0.1:8080/@@hello2
This section explain creating a dynamic hello world application.
In the src/mynamespace/main/hello.py file, add few lines of Python code like this:
class Hello(object):
def getText(self):
name = self.request.get('name')
if name:
return "Hello %s !" % name
else:
return "Hello ! What's your name ?"
This class defines a browser view in charge of displaying some content.
Now you need a page template to render the page content in html. So let’s add a hello.pt in the src/mynamespace/main directory:
<html>
<head>
<title>hello world page</title>
</head>
<body>
<div tal:content="view/getText">
fake content
</div>
</body>
</html>
The tal:content directive tells zope to replace the fake content of the tag with the output of the getText method of the view class.
The next step is to associate the view class, the template and the page name. This is done with a simple XML configuration language (ZCML). Edit the existing file called configure.zcml and add the following content before the final </configure>:
<browser:page name="hello3"
for="*"
class=".hello.Hello"
template="hello.pt"
permission="zope.Public" />
This declaration means: there is a web page called hello3, available for any content, managed by the view class Hello, rendered by the template hello.pt, and this page is public.
Since you are using the browser XML namespace, you need to declare it in the configure directive. Modify the first lines of the configure.zcml file so it looks like this (You can skip this step if the browser namespace is already there from the static hello world view):
<configure
xmlns="http://namespaces.zope.org/zope"
xmlns:browser="http://namespaces.zope.org/browser">
Restart your application, then visit the following URL: http://127.0.0.1:8080/@@hello3
You should then see the following text in your browser:
Hello ! What's your name ?
You can pass a parameter to the Hello view class, by visiting the following URL: http://127.0.0.1:8080/@@hello3?name=World
You should then see the following text:
Hello World !
This chapter walked through the process of getting started with web application development with BlueBream. Also introduced few simple Hello World example applications. The Tutorial — Part 1 chapter will go through a bigger application to introduce more concepts.
blog comments powered by Disqus