AjaxSuggest.py :  » Web-Frameworks » Webware » Webware-1.0.2 » WebKit » Examples » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Web Frameworks » Webware 
Webware » Webware 1.0.2 » WebKit » Examples » AjaxSuggest.py
#
# Ajax "Suggest" Example
#
# Written by Robert Forkel based on wiki.w4py.org/ajax_in_webware.html
# and www.dynamicajax.com/fr/AJAX_Suggest_Tutorial-271_290_312.html,
# with minor changes made by Christoph Zwerschke.
#

from random import randint

from AjaxPage import AjaxPage

maxSuggestions = 10
maxWords = 5000
maxLetters = 5

# Create some random "words":
suggestions = []
for i in range(maxWords):
  word = []
  for j in range(maxLetters):
    word.append(chr(randint(97, 122)))
  suggestions.append(''.join(word))


class AjaxSuggest(AjaxPage):

  _clientPolling = None # we have no long-running queries

  def writeJavaScript(self):
    AjaxPage.writeJavaScript(self)
    self.writeln('<script type="text/javascript" src="ajaxsuggest.js"></script>')

  def writeStyleSheet(self):
    AjaxPage.writeStyleSheet(self)
    self.writeln('<link rel="stylesheet" href="ajaxsuggest.css" type="text/css">')

  def htBodyArgs(self):
    return AjaxPage.htBodyArgs(self) + ' onload="initPage();"'

  def writeContent(self):
    self.writeln('<h2>Ajax "Suggest" Example</h2>')
    if self.request().hasField('query'):
      self.writeln('''
<p>You have just entered the word <b class="in_red">"%s"</b>.</p>
<p>If you like, you can try again:</p>'''
        % self.htmlEncode(self.request().field('query')))
    else:
      self.writeln('''
<p>This example uses Ajax techniques to make suggestions
based on your input as you type.</p>
<p>Of course, you need a modern web browser with
JavaScript enabled in order for this to work.</p>
<p>Start typing in some lowercase letters,
and get random words starting with these characters suggested:</p>''')
    self.writeln('''<form><div>
<input type="text" name="query" id="query" onkeyup="getSuggestions();" autocomplete="off">
<input type="submit" value="Submit"></div><div class="hide" id="suggestions"></div></form>''')

  def exposedMethods(self):
    """Register the suggest method for use with Ajax."""
    return ['suggest']

  def suggest(self, prefix):
    """We return a JavaScript function call as string.

    The JavaScript function we want called is `handleSuggestions`
    and we pass an array of strings starting with prefix.

    Note: to pass more general Python objects to the client side, use JSON,
    e.g. using json-py's (sourceforge.net/projects/json-py/) JsonWriter.

    """
    s = filter(lambda w, prefix=prefix:
      w.startswith(prefix), suggestions) or ['none']
    return "handleSuggestions([%s]);" % ",".join(
      map(lambda w: "'%s'" % w, s[:maxSuggestions]))
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.