EWD Python Tutorial Part 1
19 pages
English

EWD Python Tutorial Part 1

Le téléchargement nécessite un accès à la bibliothèque YouScribe
Tout savoir sur nos offres
19 pages
English
Le téléchargement nécessite un accès à la bibliothèque YouScribe
Tout savoir sur nos offres

Description

Developing Ajax Applications using EWD and Python
Tutorial: Part 1
Chapter 1: Introduction
Background
Enterprise Web Developer (EWD) is a framework for developing and running sophisticated and
secure Ajax applications. EWD's key design objective was extremely rapid and simple
development, combined with extremely easy maintenance, making it the most productive Ajax
development platform currently available.
EWD differs markedly from most of the most well-known Ajax frameworks in that it eschews the
programmer-orientated Model/View Controller(MVC) paradigm and instead places the designer at
the focus of the development process, with programming reduced to a relatively trivial aspect of the
overall process. The majority of the work that would be required in an MVC-based framework is
automated in EWD, leaving the development team able for focus their time and efforts on
describing what the application should do rather than how works.
This tutorial aims to demonstrate how quickly and easily an Ajax application can be developed
using EWD. EWD applications can be developed using a variety of languages, and this tutorial
focuses on the use of Python.
Prerequisites
You'll need a Linux system with GT.M and EWD installed. The quickest and easiest way to do this
is to use the M/DB Installer ( http://gradvs1.mgateway.com/main/index.html?
path=mdb/mdbDownload ). The M/DB Appliance that the installer builds includes a pre-configured,
ready to run EWD and GT.M ...

Sujets

Informations

Publié par
Nombre de lectures 121
Langue English

Exrait

Developing Ajax Applications using EWD and Python Tutorial: Part 1
Chapter 1: Introduction Background Enterprise Web Developer (EWD) is a framework for developing and running sophisticated and secure Ajax applications. EWD's key design objective was extremely rapid and simple development, combined with extremely easy maintenance, making it the most productive Ajax development platform currently available. EWD differs markedly from most of the most well-known Ajax frameworks in that it eschews the programmer-orientated Model/View Controller(MVC) paradigm and instead places the designer at the focus of the development process, with programming reduced to a relatively trivial aspect of the overall process. The majority of the work that would be required in an MVC-based framework is automated in EWD, leaving the development team able for focus their time and efforts on describing what the application should do rather than how works. This tutorial aims to demonstrate how quickly and easily an Ajax application can be developed using EWD. EWD applications can be developed using a variety of languages, and this tutorial focuses on the use of Python.
Prerequisites You'll need a Linux system with GT.M and EWD installed. The quickest and easiest way to do this is to use the M/DB Installer ( http://gradvs1.mgateway.com/main/index.html? path=mdb/mdbDownload ). The M/DB Appliance that the installer builds includes a pre-configured, ready to run EWD and GT.M environment, complete with the Python utilities we'll use in this tutorial. This document will assume you're using a default EWD environment as configured in the M/DB Appliance. Adjust any path references etc as required if you're using a different EWD set-up. If you're developing from within a Windows environment, my recommendation is to use WinSCP ( http://winscp.net/eng/index.php ) to create and edit your EWD pages on your Linux system, and use puTTY ( http://www.chiark.greenend.org.uk/~sgtatham/putty/ ) to execute code on the Linux system. You'll also need a standard web browser to run the applications that you'll be building with EWD and Python. Let's get started!
Developing Ajax Applications using EWD and Python: Tutorial Part 1 27 November 2009. Ó 2009, M/Gateway Developments Ltd. All Rights Reserved 1
Chapter 2: A Simple Hello World
No tutorial is complete without an initial Hello World application, so let's build one with EWD. First let's create a directory to hold the source pages that we'll be writing in this tutorial. The directory I'll be using is /usr/ewdapps/tutorial . Assuming you're using the M/DB Appliance, you'll find that the directory /usr/ewdapps already exists, so just add tutorial, eg from the command line:
cd /usr/ewdapps mkdir tutorial You may need to set the permissions, eg: sudo chmod 755 tutorial
Now create a file named helloworld.ewd containing the following text and save it in the tutorial directory <ewd:config> <html> <head> <title>EWD Python Tutorial: Hello World</title> </head> <body> <p>Hello World!</p> </body> </html> In order to run this Hello World application, we must first compile it using EWD. Start a terminal session, switch to the directory /usr/local/gtm/ewd and enter the Python shell:
rob@ubuntu910-mdb:/$ cd /usr/local/gtm/ewd rob@ubuntu910-mdb:/usr/local/gtm/ewd$ python Python 2.6.4rc2 (r264rc2:75497, Oct 20 2009, 02:55:11) [GCC 4.4.1] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> We need to import the ewd.py module: >>> import ewd At this point you'll find that you've lost character echo. To restore it, so you can see what you're doing, type in the following: >>> import subprocess >>> subprocess.call("stty echo",shell=True) OK now that's done we're ready to go. Compile the application by entering:
Developing Ajax Applications using EWD and Python: Tutorial Part 1 27 November 2009. Ó 2009, M/Gateway Developments Ltd. All Rights Reserved 2
>>> ewd.compileAll("tutorial") /usr/ewdapps/tutorial/ewdAjaxError.ewd /usr/ewdapps/tutorial/ewdAjaxErrorRedirect.ewd /usr/ewdapps/tutorial/ewdErrorRedirect.ewd /usr/ewdapps/tutorial/helloworld.ewd >>>
Now you can run it in a browser by using the URL:
http://[ipAddress]/ewd/tutorial/helloworld.ewd
where ipAddress is the IP Address or domain name allocated to the M/DB Appliance, eg
http://192.168.1.127/ewd/tutorial/helloworld.ewd
You should see the Hello World! Message.
Developing Ajax Applications using EWD and Python: Tutorial Part 1 27 November 2009. Ó 2009, M/Gateway Developments Ltd. All Rights Reserved 3
Chapter 3: Hello World Mk II OK well that wasn't particularly spectacular: nothing more than you could have done with a simple static page of HTML, but at least we can see that EWD is working and ready to use. Let's enhance that simple Hello World application a little and make the text come from what we term a Session variable instead. We'll create the Session variable using a Python function. In the same directory as before ( /usr/ewdapps/tutorial ), create a text file named helloworld2.ewd containing the following: <ewd:config prepagescript="py:tutorial.getHelloworldData"> <html> <head> <title>EWD Python Tutorial: Hello World MkII</title> </head> <body> <p><?= #helloworld ?></p> </body> </html> What we've done is to add a pre-page script : a Python function that will be executed before the page is generated and sent to the browser. In this case the function is named getHelloworldData() and will reside in a module named tutorial.py . Notice the other change: instead of the hard-coded text “Hello World!”, we're referencing a variable named helloworld . In this case it's what's known as an EWD Session Variable , denoted by the # prefix, ie <?= #helloworld ?>. The purpose of the pre-page script will be to create that session variable: we'll see how to do that in a minute. First recompile the tutorial application from your Python shell again. You'll probably find that you've lost character echo again. You can reset it by typing: subprocess.call("stty echo",shell=True) Then type: >>> ewd.compileAll("tutorial") /usr/ewdapps/tutorial/ewdAjaxError.ewd /usr/ewdapps/tutorial/ewdAjaxErrorRedirect.ewd /usr/ewdapps/tutorial/ewdErrorRedirect.ewd /usr/ewdapps/tutorial/helloworld.ewd /usr/ewdapps/tutorial/helloworld2.ewd >>> You'll see that both your pages (helloworld.ewd and helloworld2.ewd) have been compiled. Of course, before we can run your new version, we have to create that Python module named tutorial.py that will contain the pre-page script function we referenced in our EWD page helloworld2.ewd So, create a file named tutorial.py containing the following text and save it in the /usr/local/gtm/ewd path:
Developing Ajax Applications using EWD and Python: Tutorial Part 1 27 November 2009. Ó 2009, M/Gateway Developments Ltd. All Rights Reserved 4
import ewd def getHelloworldData(sessid): ewd.setSessionValue("helloworld","Hello world, this time from an EWD Session variable!",sessid) return ""
Now try running the application in the browser. Use the URL: http://[ipAddress]/ewd/tutorial/helloworld2.ewd This time you should see the text: Hello world, this time from an EWD Session variable!
Let's take a closer look at this example and examine what happened when we ran it. The small additions we made to our original Hello World page actually introduce quite a few important EWD concepts, so it's worth reading the following section.
Developing Ajax Applications using EWD and Python: Tutorial Part 1 27 November 2009. Ó 2009, M/Gateway Developments Ltd. All Rights Reserved 5
Chapter 4: Relevant Background Information on EWD Python EWD Functions All Python functions used in EWD have the same calling signature: they have a single input parameter sessid that will be passed into it at run-time by EWD. The sessid variable contains the unique EWD-generated Session ID for the user. The main purpose of Python functions that you'll write for your EWD applications is to move data between your database and the EWD Session. The EWD Session is a pool of data that you can create for the user, and, once created, is automatically persisted inside EWD's GT.M database for the duration of the user's session. The Session ID (sessid) provides the unique pointer to the specific user who is executing the function at the time. Your Python functions manipulate the EWD Session via a set of EWD-provided functions, each of which requires this Session ID as a parameter. The EWD Session The user's EWD session is created when he/she invokes an EWD page that is denoted to be a “first page” and is destroyed when one of two things happen:  the user clicks a link that explicitly terminates the EWD session the user stops interacting with the application for more than the Session Timeout limit (which by default is 20 minutes, but can be set to any value you like). Any data stored in the user's EWD session data storage container (known as the EWD Session) is potentially available for use in any of the pages that you'll present to the user throughout the lifetime of that user's session.
The EWD Session therefore provides a middle tier of data storage between the pages that define what the user will see and the database that ultimately contains the data that the pages will be manipulating.
Developing Ajax Applications using EWD and Python: Tutorial Part 1 27 November 2009. Ó 2009, M/Gateway Developments Ltd. All Rights Reserved 6
In the EWD model, the role of the designer is cleanly separated from that of the programmer. Indeed the role of the programmer is solely related to moving data from the database to the EWD Session and back again, in addition to back-end validation of data prior to storage.
EWD Separates Design from Programming
In the EWD model, the role of the designer is to design and write the HTML and associated Javascript and CSS coding that describes the behaviour of each page. From the designer's perspective, the only thing that appears to distinguish an EWD application from a set of static web pages is that he/she has access to variable information that resides in the Session and appears to be accessible in his/her pages. How that data got there and how it gets back into a database is largely irrelevant to the designer: that's the role of the programmer.
Conversely, the programmer neither particularly knows nor cares how or where the data that is being moved into the Session will be used in the page(s) that require it. The programmer writes Python functions that are triggered by the designer's pages at the appropriate times. The main tasks of the programmer, therefore are:
fetching data from the database and placing them in the EWD Session ready for use in one or more pages; validating and saving data back into the database, in this case data that has been moved into the EWD Session as a result of, for example, an HTML form being submitted.
This clear separation of the database and its associated schema from the web pages that use and manipulate that data may seem unusual to those used to typical contemporary mainstream web application MVC paradigms: in EWD there is no concept of data binding. This is deliberate and contrary to what might be thought, actually proves remarkably beneficial in many ways that we will see in due course.
We've been describing the programmer and designer as two distinct people, but of course in many
Developing Ajax Applications using EWD and Python: Tutorial Part 1 27 November 2009. Ó 2009, M/Gateway Developments Ltd. All Rights Reserved 7
cases, they will be one and the same person, as will be the case when you're trying out this tutorial. However in EWD, we can consider the work involved in developing an application to be split into these two roles, whether real or notional. This tutorial will refer to the Designer or Programmer where appropriate, even though you'll be wearing both hats!
The Interface Between Designer and Programmer Clearly for the EWD model to work, there has to be some meeting point between Designer and Programmer. We've actually seen this in our Hello World MkII page. It was the reference at the top of the page to the Pre-page Script : <ewd:config prepagescript="py:tutorial.getHelloworldData">
Every EWD page must include an <ewd:config> tag, which, by convention, usually is placed at the top of the page. The <ewd:config> tag is used for a variety of purposes as we will see, but its key use in this example is as the place-holder for the reference to a Python function that will be invoked before the page is generated and sent to the browser. The Designer does not really need to know what the Pre-page Script does. However, he/she will have determined, during the design of the specific page, that the page requires some variable data that must have originally come from the database. So he/she will usually have a good idea that a Pre-page script is needed to fetch that data and make it available. The actual name of the Python function and the name of the module it resides in will have to be agreed with or determined by the Programmer. In our example, the “Designer” decided to display the value of a Session Variable named helloworld: <p><?= #helloworld ?></p>
The Programmer has written a Python function, getHelloworldData() and placed it in a module named tutorial.py . This Python module must reside in the same directory as the GT.M database environment in which EWD is running, in our case /usr/local/gtm/ewd, Our Python Pre-Page Script Function Revisited Let's now take another more detailed look at that Python function we used as our Pre-page Script: import ewd def getHelloworldData(sessid): ewd.setSessionValue("helloworld","Hello world, this time from an EWD Session variable!",sessid) return ""
At the top of the module we import ewd.py because this provides us with the EWD Session management APIs we'll need to use. The function getHelloworldData() has a single parameter, sessid , which is the EWD Session ID.
Developing Ajax Applications using EWD and Python: Tutorial Part 1 27 November 2009. Ó 2009, M/Gateway Developments Ltd. All Rights Reserved 8
The ewd.setSessionValue() function was used to instantiate an EWD Session Variable named helloworld, with the value defined by the second parameter. The third parameter is the EWD Session ID. The function finally returns a null string. This indicates to EWD that the function ran to completion without error. In this simple example, the value for the EWD Session Variable was hard-coded as a literal string value, but in a proper application, the value would probably have been retrieved from the database. However the value is obtained, the result is that after this Pre-page script function has completed, an EWD Session Variable named helloworld will now exist and can therefore be referenced in the page using the syntax: <?= #helloworld ?> When the page runs, EWD will substitute the actual value of this EWD Session Variable.
What Database can you use with EWD? The answer is any database that can be supported by Python! Of course the M/DB Appliance provides you with two that you could use: M/DB which is a SimpleDB clone, and GT.M, a schema-less NoSQL Database. However you can use any other database you wish. EWD itself will know nothing about your database: it only concerns itself with data in the EWD Session. When you move data into the EWD Session, you're actually making use of a GT.M database, but this all happens behind the scenes. All your access to and manipulation of the EWD Session is done using the EWD Session APIs that are available in the ewd.py module.
The EWD Compiler The EWD pages that the Designer creates are simply an abstracted description of the pages he/she wants to have delivered to the user's browser. EWD's compiler converts those pages into GT.M native language routines. The M/DB Appliance includes a pre-configured EWD/GT.M environment that you can treat as a “black box” that runs the EWD run-time framework. If you're interested you can examine the routines that are generated from your pages: you'll find them in the /usr/local/gtm/ ewd directory with a naming convention of ewdWL[applicationName][pageName].m . We've already seen how EWD's compiler can be invoked from the Python shell to compile all pages in an application directory. You can also compile individual pages: >>> ewd.compilePage("tutorial","helloworld2") /usr/ewdapps/tutorial/ewdAjaxError.ewd /usr/ewdapps/tutorial/ewdAjaxErrorRedirect.ewd /usr/ewdapps/tutorial/ewdErrorRedirect.ewd /usr/ewdapps/tutorial/helloworld2.ewd >>>  
Developing Ajax Applications using EWD and Python: Tutorial Part 1 27 November 2009. Ó 2009, M/Gateway Developments Ltd. All Rights Reserved 9
There are two further ways of compiling pages: using the GT.M shell using EWD's web portal application. Using the GT.M Shell The GT.M shell is similar to the Python shell, but you'll use GT.M's native language commands instead of Python ones. You can read about this language elsewhere: here we'll just mention the commands you'll need for this tutorial. To start the GT.M shell:
rob@ubuntu910-mdb:~$ cd /usr/local/gtm/ewd rob@ubuntu910-mdb:/usr/local/gtm/ewd$ $gtm GTM> To exit the GT.M shell, type H or h followed by Enter: GTM> h rob@ubuntu910-mdb:/usr/local/gtm/ewd$ From the GT.M shell you can compile all the pages in an EWD Application directory as follows:
GTM> d compileAll^%zewdAPI("tutorial") /usr/ewdapps/tutorial/ewdAjaxError.ewd /usr/ewdapps/tutorial/ewdAjaxErrorRedirect.ewd /usr/ewdapps/tutorial/ewdErrorRedirect.ewd /usr/ewdapps/tutorial/helloworld.ewd /usr/ewdapps/tutorial/helloworld2.ewd GTM> Or you can compile a single page in an EWD Application:
GTM> d compilePage^%zewdAPI("tutorial","helloworld2") /usr/ewdapps/tutorial/ewdAjaxError.ewd /usr/ewdapps/tutorial/ewdAjaxErrorRedirect.ewd /usr/ewdapps/tutorial/ewdErrorRedirect.ewd /usr/ewdapps/tutorial/helloworld2.ewd GTM> You'll find that the GT.M shell provided command-line recall: to repeat a previous command just press the up-arrow key and press Enter. This makes the GT.M shell very convenient when you're developing a page and need to repeatedly edit and re-compile it.
Developing Ajax Applications using EWD and Python: Tutorial Part 1 27 November 2009. Ó 2009, M/Gateway Developments Ltd. All Rights Reserved 10
The EWD Web Portal The M/DB Appliance includes a pre-built EWD Ajax application that provides a web-based portal application for managing many of the day-to-day operations surrounding an EWD environment. This application, named ewdMgr , is also useful in that it demonstrates many of EWD's advanced Ajax capabilities. You'll find the source EWD pages for it in the /usr/ewdapps/ewdMgr directory, but you'll notice that the pre-page and other scripts used by ewdMgr are not written in Python but use GT.M's native language instead. However, there is nothing done in these scripts that couldn't also be done using Python. To start the ewdMgr application, use the following URL in your browser: http://[ipAddress]/ewd/ewdMgr/gtmHome.ewd where ipAddress is the IP address or domain name allocated to your M/DB Appliance
Note: if you get a page telling you that you aren't allowed access to this page, you can reset the security as follows: From the GT.M shell type the following:
GTM> d resetSecurity^%zewdGTM
Now try that URL again, but add a ? at the end to ensure the browser doesn't just return a cached version of the page, ie: http://[ipAddress]/ewd/ewdMgr/gtmHome.ewd? You'll find that from the ewdMgr application you can compile/recompile individual pages or entire applications. One of its really useful facilities is that it allows you to easily view the contents of the EWD Session for specified Session IDs: this is an extremely useful debugging tool during development. Also within the ewdMgr application you'll find a lot of detailed documentation, tutorials etc that you'll find useful as you become more familiar with EWD.
OK that's enough background detail for now. Let's get back to the tutorial and examine the other key mechanism in EWD: page navigation.
Developing Ajax Applications using EWD and Python: Tutorial Part 1 27 November 2009. Ó 2009, M/Gateway Developments Ltd. All Rights Reserved 11
  • Accueil Accueil
  • Univers Univers
  • Ebooks Ebooks
  • Livres audio Livres audio
  • Presse Presse
  • BD BD
  • Documents Documents