transpilers

Base transpiler components.

render_static.transpilers.base.to_js(value)[source]

Default javascript transpilation function for values. Simply adds quotes if it’s a string and falls back on json.dumps() for non-strings and non- numerics.

Parameters:

value (Any) – The value to transpile

Returns:

Valid javascript code that represents the value

Return type:

str

render_static.transpilers.base.to_js_datetime(value)[source]

A javascript value transpilation function that transpiles python dates and datetimes to javascript Date objects instead of strings. To use this function in any of the transpilation routines pass it to the to_javascript parameter on any of the template tags:

{% ... to_javascript="render_static.transpilers.to_js_datetime" %}
Parameters:

value (Any) – The value to transpile

Returns:

Valid javascript code that represents the value

Return type:

str

class render_static.transpilers.base.CodeWriter[source]

Bases: object

A base class that provides basic code writing functionality. This class implements a simple indentation/newline scheme that deriving classes may use.

__init__(level=0, indent='    ', prefix='', **kwargs)[source]
Parameters:
  • level (int) – The level to start indentation at

  • indent (str | None) – The indent string to use

  • prefix (str) – A prefix string to add to each line

  • kwargs – Any additional configuration parameters

Return type:

None

classmethod __new__(*args, **kwargs)
class render_static.transpilers.base.Transpiler[source]

Bases: CodeWriter

An abstract base class for JavaScript generator types. This class defines a basic generation API, and implements configurable indentation/newline behavior. It also offers a toggle for ES5/ES6 mode that deriving classes may use.

To use this class derive from it and implement include_target() and visit().

__init__(to_javascript=<function to_js>, overrides=None, **kwargs)[source]
Parameters:
  • to_javascript (str | Callable) – A callable that accepts a python artifact and returns a transpiled object or primitive instantiation.

  • kwargs – A set of configuration parameters for the generator, see above.

  • overrides (Dict[str, OverrideNode] | None)

Return type:

None

classmethod __new__(*args, **kwargs)
property Transpiler.context

The base template render context passed to overrides. Includes:

  • transpiler: The transpiler instance