question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Importing a Brython module that is implemented in pure JavaScript

See original GitHub issue

I hope you had a wonderful Christmas 🎄 🎅 🤶 🎁

I have the special case that I have a Brython module in pure JavaScript and want to import it using the import statement without making it accessible via a script tag beforehand.

Thanks in advance.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
PierreQuentelcommented, Dec 29, 2020

I don’t think it’s a good idea to change the standard Brython import mechanism in py_import.js, because it would add the search for JS modules (hence an Ajax call) for all imports. At the moment, only the JS modules in the Brython stdlib, in folder libs, can be imported.

My idea was more to develop a specific loader, that would only be added to sys.meta_path for the (rare) case when a user-specific JS module, at a specific location, needs to be imported.

In issue #1182 I had provided an example of a loader written in Python, maybe you can start from this example for a Javascript module loader.

0reactions
PierreQuentelcommented, May 17, 2022

In commit 8e8d0a59edd925acabda4a09fccda6deff2334fb I have added a function import_js(url, name) in module javascript that makes it possible to import a module written in Javascript. This module must define a name $module, used to build the module namespace, with the usual Javascript -> Python conversions.

If Javascript script at js_test.js is

var $module = { x: 1 }

then a Python script can use it this way:

import javascript

javascript.import_js("js_test.js", "js_module") 
import js_module 
assert js_module.x == 1
Read more comments on GitHub >

github_iconTop Results From Across the Web

Implementation of import - Brython documentation
To import modules or packages, Brython uses the same mechanism as CPython : to resolve "import X", the program looks for a file...
Read more >
An Introductory Guide to Brython - Stack Abuse
In this introduction to Brython, we'll take a look at what it is, how to install it, and how to initialize a Brython...
Read more >
brython/README.md at master - GitHub
Brython supports the syntax of Python 3, including comprehensions, generators, metaclasses, imports, etc. and many modules of the CPython distribution.
Read more >
Brython: Python in Your Browser - Real Python
You may not like being forced to learn and use JavaScript to implement a web ... Line 10 imports browser , a Brython...
Read more >
How to Use Brython: A Guide to Writing Python for the Front End
While Brython is Python under the hood, it is an implementation of JavaScript. You cannot have a pure Python frontend application. Why? Because ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found