-
-
Notifications
You must be signed in to change notification settings - Fork 328
module_from_template API changes
#784
-
The core idea is that module_from_template is currently unsustainable. So I'm proposing a new API that should feel like JavaScript.
For now, let's focus on React modules to limit scope creep.
I would envision the cleanest user workflow to look like this
Step 1
# Set a dir where NPM modules will be found. # This should not default to tmp storage os.environ["IDOM_NPM_MODULE_DIR"] = "my_app/js/node_modules"
Step 2
# Install a module into IDOM_NPM_MODULE_DIR npm install react-bootstrap
Step 3
# Import the module into Python using a react namespaced package # This API should feel like the JavaScript API of # import Button from 'react-bootstrap/Button'; button = idom.react_module("import Button from 'react-bootstrap'")
# We might also want to support list/set for `name` to mimic the following # import { Button } from 'react-bootstrap'; button, not_a_button = idom.react_module("import { Button, NotAButton } from 'react-bootstrap'")
# Here's the all the API parameters button = idom.react_module( "import { Button } from 'react-bootstrap'", fallback=None, unmount_before_update=False, ) # Notably, the following args no longer exist # resolve_exports: Should always be resolved, similar to JS `import` behavior # resolve_exports_depth: Should use an exhaustive depth search, within the namespace of the module # symlink: Symlink should always be attempted, and fallback to a copy operation if an exception occurs
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 1 comment 4 replies
-
Unfortunately a simple npm install won't be sufficient. There needs to be a build step to translate any CommonJS to ECMAScript. So we'd need to create some wrapper CLI around npm and a build tool like Rollup. In many ways, the process for building these modules would be very similar to making a custom javascript component using the template repository.
I'd like to avoid bringing back the old idom install CLI into core (for now at least). What if we provided this as a separate idom-build package whose usage would be:
$ idom-build react-bootstrap
Discovered 'react' as a peer dependency. Is this a React component library (yes/no)? yes
Building 'react-bootstrap' module...
Where this would create a react-bootstrap.js file. Then in your Python:
from idom import web react_bootstrap = web.module_from_file("./react-bootstrap.js") button = web.export(react_bootstrap, "Button")
Beta Was this translation helpful? Give feedback.
All reactions
-
I'm okay with idom-build react-bootstrap as a separate package.
I would still recommend the Python API changes. The whole IDOM framework is very much 1:1 with JS/react, except for module_from_x.
Beta Was this translation helpful? Give feedback.
All reactions
-
I'm not really convinced that writing a JS import statement is really the best interface. I'd also like to retain the ability to load modules from strings, files and URLs. Perhaps things could be cleaned up by allowing module_from_x(...).export('Component') or module_from_x(...).Component? The latter could be made to work after this change.
Beta Was this translation helpful? Give feedback.
All reactions
-
The latter is a bit awkward due to type hinting errors.
We may as well just tweak the default kwargs and use the interface you suggested above.
Beta Was this translation helpful? Give feedback.
All reactions
-
However, I do disagree with the name export in general, even with current versions of IDOM.
I know that from the framework's perspective it's an export. But, from a programmer's perspective, it's an import.
Beta Was this translation helpful? Give feedback.