jToolkit Tutorial

hosted by SourceForge.net Logo

Quick Start and Sample Code:

Here is the simple "Hello, World!" demo included with the code... (in the demo directory). Ask on the users list for help...

.htaccess file

SetHandler python-program
PythonHandler jToolkit.web
PythonOption jToolkit.module jToolkit.demo.helloworld
PythonOption jToolkit.instance HelloWorldConfig
PythonDebug On
  

Note that the line

PythonOption jToolkit.module jToolkit.demo.helloworld
specifies the jToolkit.demo.helloworld module. To specify your own module, replace this with
PythonOption jToolkit.module <your module name>

helloworld.py

"""this is a Hello World-style demonstration program of jToolkit"""

from jToolkit.web import server
from jToolkit.widgets import widgets

class HelloWorldServer(server.AppServer):
  """the Server that serves the Hello World Pages"""
  def getpage(self, pathwords, session, argdict):
    """return a page that will be sent to the user"""
    return widgets.PlainContents("Hello World")

class HelloWorldConfig:
  """the configuration parameters for a hello world server"""
  serverclass = HelloWorldServer

Database example

This expands the "Hello, World!" demo to demonstrate a connection to an Access database, and the displaying of a table on an HTML page. The resulting "helloworld.py" file is listed at the end of this section.

DBAppServer

server.DBAppServer inherits from server.AppServer. It has an attribute instance.db, which is a database connection using properties specified in the Config class (jToolkit.instance in the .htaccess file).

To specify the Server as a DBAppServer, add the import

from jToolkit.data import ADOProviders
change the class header to
class HelloWorldServer(server.DBAppServer):
and add the following attributes to HelloWorldConfig:
  DBTYPE = "access"
  DBNAME = "d:/temp/jToolkit.mdb"
  DBHOST = ""
  DBUSER = "admin"
  DBPASSWORD = ""
  DBPROVIDER = ADOProviders.AccessProvider
  tracefile = "d:/temp/jToolkit.log"
where DBTYPE is one of "access","sqlserver", "oracle" or "postgres"
DBNAME is the name of the database (including full path, in the case of Access)
DBHOST is the host name (only relevant to SQLServer)
DBUSER is the user name
DBPASSWORD is the password
DBPROVIDER is a valid provider for the database type (ADOProviders also provides SQLServerProvider and OracleProvider)
and tracefile is the logfile for errors and tracebacks.

Widgets

So far, the server will connect to a database, but will not retrieve or display anything from it. This section deals with three objects - dbtable.DBTable, which retirves a table from a database; grid.SimpleGrid, which displays a table as an HTML table; and widgets.Page, which provides the normal encapsulating tags for HTML pages.

Database helloworld.py

"""this is a demonstration program for jToolkit database access"""

from jToolkit.web import server
from jToolkit.widgets import widgets, grid
from jToolkit.data import ADOProviders, dbtable

class HelloWorldServer(server.DBAppServer):
  """the Server that serves the Hello World Pages"""
  def getpage(self, pathwords, session, argdict):
    """return a page that will be sent to the user"""
    testTable = dbtable.DBTable(self.instance.db, "Prices",["id","vendor","product","type","price"],"id",["type","product","vendor"])
    testGrid = grid.SimpleGrid(testTable,[("vendor","Vendor",""),("product","Product",""),("type","Type",""),("price","Price","")])
    return widgets.Page("Test database connection",[testGrid])

class HelloWorldConfig:
  """the configuration parameters for a hello world server"""
  serverclass = HelloWorldServer
  DBTYPE = "access"
  DBNAME = "d:/temp/jToolkit.mdb"
  DBHOST = ""
  DBUSER = "admin"
  DBPASSWORD = ""
  DBPROVIDER = ADOProviders.AccessProvider
  tracefile = "d:/temp/jToolkit.log"

Links:

Other sections on this web page: