{"id":2304,"library":"telepath","title":"Telepath","description":"Telepath is a Python library designed for exchanging data between Python and JavaScript. It enables Python objects to be packed into primitive types suitable for frontend consumption, typically within a web context. The current version is 0.3.1. Releases are infrequent, usually tied to specific feature additions or compatibility updates, particularly concerning Django versions.","status":"active","version":"0.3.1","language":"en","source_language":"en","source_url":"https://github.com/wagtail/telepath","tags":["javascript","data-exchange","frontend","wagtail","python-js"],"install":[{"cmd":"pip install telepath","lang":"bash","label":"Install Telepath"}],"dependencies":[],"imports":[{"symbol":"Adapter","correct":"from telepath import Adapter"},{"symbol":"register","correct":"from telepath import register"},{"symbol":"JSContext","correct":"from telepath import JSContext"},{"symbol":"pack","correct":"from telepath import pack"}],"quickstart":{"code":"from telepath import Adapter, register, JSContext, pack\n\n# 1. Define a custom Python object\nclass MyCustomObject:\n    def __init__(self, message, value):\n        self.message = message\n        self.value = value\n\n# 2. Define a Telepath adapter for your custom object\nclass MyCustomObjectAdapter(Adapter):\n    js_constructor = 'telepath.MyCustomObject'\n\n    def pack(self, obj, context):\n        # Pack the Python object into a list of primitive types\n        return [obj.message, obj.value]\n\n# 3. Register the adapter with the custom object\nregister(MyCustomObject, MyCustomObjectAdapter)\n\n# 4. Create an instance of your custom object\nmy_instance = MyCustomObject(\"Hello from Python!\", 123)\n\n# 5. Create a JSContext and pack the object\ncontext = JSContext()\npacked_data = pack(my_instance, context)\n\nprint(\"--- Python Side ---\")\nprint(f\"Original object: {my_instance.message}, {my_instance.value}\")\nprint(f\"Packed data: {packed_data}\")\n\n# The context provides the necessary JavaScript to unpack the data on the frontend.\n# This script tag should be included in your HTML.\nprint(\"\\n--- Frontend Integration (JavaScript) ---\")\nprint(\"Include this script tag in your HTML header:\")\nprint(context.as_script_tag())\n\nprint(\"\\nOn the frontend, with telepath-unpack.js loaded, you would then do:\")\nprint(\"// const myUnpackedObject = window.telepath.unpack(packed_data);\")\nprint(\"// console.log(myUnpackedObject.message); // 'Hello from Python!'\")","lang":"python","description":"This quickstart demonstrates how to define a custom Python object, create a Telepath adapter for it, register the adapter, and then pack an instance of the object into a JavaScript-compatible format. It also shows how to generate the necessary JavaScript context for the frontend to unpack the data using the companion `telepath-unpack.js` library."},"warnings":[{"fix":"Upgrade your Python environment to 3.8+ and Django to 3.2+, or restrict your `telepath` dependency to `<0.3.1`.","message":"Version 0.3.1 dropped support for Python versions older than 3.8 and Django versions older than 3.2. Projects on older environments must either upgrade Python/Django or pin telepath to an earlier version.","severity":"breaking","affected_versions":"<0.3.1"},{"fix":"Ensure `telepath-unpack.js` is loaded in your HTML, typically via a `<script>` tag, before attempting to unpack data on the frontend.","message":"Telepath handles packing Python objects into primitive types. For client-side unpacking, you must include the `telepath-unpack.js` library in your frontend assets.","severity":"gotcha","affected_versions":"All"},{"fix":"Embed the output of `context.as_script_tag()` in the `<head>` or at the start of `<body>` of your HTML page.","message":"The `JSContext` object generated on the Python side contains crucial information for the frontend to correctly re-construct your Python objects. Its script tag (`context.as_script_tag()`) must be rendered into your HTML.","severity":"gotcha","affected_versions":"All"},{"fix":"Refactor custom object packing logic to use the `telepath.Adapter` class and `register()` function for clearer and more maintainable code.","message":"While older manual packing methods might still function, Telepath v0.2 and v0.3 introduced and refined an API allowing classes to define their own packing logic (via the `Adapter` class and `pack` method). The recommended approach is to use this dedicated adapter pattern.","severity":"deprecated","affected_versions":"<0.2"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}