""" most code taken and adapted from http://wiki.w4py.org/ajax_in_webware.html and http://www.dynamicajax.com/fr/AJAX_Suggest_Tutorial-271_290_312.html author: robert forkel """ # inherit from AjaxPage, which in turn inherits from ExamplePage from ajaxpage import AjaxPage MAX = 10 # only pass a limited number of suggestions class AjaxSuggest(AjaxPage): def writeHeadParts(self): AjaxPage.writeHeadParts(self) # write css for the suggestion box self.write(""" """) # write javascript self.write(""" """) def writeContent(self): self.write("""\

Start typing in some lowercase letters, and get words starting with these characters suggested:

""") def ajax_allowed(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. """ s = filter(lambda w: w.startswith(prefix), SUGGESTIONS) if not s: s = ['none'] # # note: to pass more general python objects to the client side, use json, # e.g. using json-py's (https://sourceforge.net/projects/json-py/) JsonWriter. # return "handleSuggestions([%s]);" % ",".join(map(lambda w: "'%s'" % w, s[:MAX])) from random import choice from string import ascii_lowercase SUGGESTIONS = [] for i in range(500): word = [] for j in range(5): word.append(choice(ascii_lowercase)) SUGGESTIONS.append(''.join(word))