1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
'''
JQuery is a powerful and easy to use Javascript library.
Paste is a powerful and easy to use Python library for Web programming.
Once in a while I need use both but I always forget the details of
their integration. This post is a note to self to remember what I did
the next time I am going to use both libraries together.
The quick and dirty way
--------------------------------------------------------
Here I will just discuss the quicker approach, suitable for prototyping
applications, not for production use. One can just perform the following
steps:
1.
easy install Paste;
2.
download the jquery.pack library and save it somewhere,
let's say in the /tmp directory;
3.
write the body of the web page you want to enhance with javascript,
and save it in /tmp;
4.
write the javascript code, and save it in /tmp;
5.
write a WSGI application serving the directory where JQuery is installed
by using ``paste.urlparser.StaticURLParser``.
However, the interesting thing is to generate the HTML code
and the Javascript code from Python and to treat the generated
page as a WSGI application. Here is an simple script doing exactly
that:
$$jquery_ex
The only nontrivial bit is the ``shift_path_info`` function, which is
part of the standard library starting from Python 2.5 (in ``wsgiref.util``).
``shift_path_info`` extracts the name of the application from the start
of the URL: in particular, if the URL starts with ``/static``, the
application dispatches to the underlying ``StaticURLParser``
application, otherwise the JQuery-enhanced HTML page specified
by ``body`` and ``js`` is returned. Notice that the source code for the
JQuery library (in compact format) can be retrieved since the HTML template
contains the line ``script type="text/javascript" src="/static/jquery.pack.js">
</script>``.
A simple page dispatcher
----------------------------------------------------------------
The previous example is simple but perhaps *too* simple. If you want to
prototype multipage applications you need some kind of ``Dispatcher`` object.
You could use of the many WSGI dispatchers you can find on PyPI, or you
could write one from scratch, since it takes only few lines. Here I
will use a custom dispatcher defined as follows:
$$Dispatcher
You instantiate the dispatcher by passing to it the path of the directory
containing the static files and a root WSGI application; you can then
add other applications via the ``.add`` method. For sake of convenience,
you can also pass a pair ``(html-body, javascript-code)`` instead of
a regular application, and the pair will be automagically converted into a WSGI
application.
Here is an example of use:
$$jquery_ex2
If you click on the title, it will slowly disappear.
'''
import jquery_ex, jquery_ex2
from jquery_helper import Dispatcher
from paste.httpserver import serve
if __name__ == '__main__':
app = Dispatcher((body, js), {'static' : '/tmp'})
serve(app, '', 8000)
|