Pylons / deform

A Python HTML form library.
Other
416 stars 160 forks source link

AutocompleteInputWidget - more powerful configuration for typeahead.js #413

Open dairiki opened 4 years ago

dairiki commented 4 years ago

This is a redo of PR #185, rebased onto the current master branch.

dairiki commented 4 years ago

Some comments extracted/duplicated from PR #185

Problem: I had been relying on a hack to support JSON-esque serialization of values containing literal javascript using json.JSONEncoder from python’s standard library. It seems that my hack is not feasible in any version of python after 2.6 (). (My hack involved overriding JSONEncoder._iterencode, which does not exist in python >= 2.7.)

My hacked up version of JSONEncoder is JSONConfigSerializer in widget.py, which works with the new literal_js marker class which is defined just above it. The two unit tests which are currently failing are a result of these hacks not working in recent versions of python.

I see two ways forward:

  1. Implement our own serializer (more or less from scratch.)
  2. Find some other external JSON/javascript-serializer that will work for us. (I’m not sure that such a thing exists.) I’m not sure that either is worth the effort.

@stevepiercy responded:

Does this thread describe the same issue https://stackoverflow.com/a/53848267/2214933?

Does jsons do what is needed?

No, I think this is a different problem. At the root is the fact that we want to be able to serialize python data structures to javascript expressions which are not necessarily valid JSON. E.g. one would like to be able to include javascript function expressions, as well as function and variable names in the serialized result.