- Fork and clone the repository to your local computer.
- Activate the appropriate conda environment.
- Run
pip install anywidget. - Open the cloned repo with your favorite IDE (e.g., VS Code).
- Run examples in the
notebooksfolder.
In the previous tutorial, you learned how to create a module by importing JavaScript and CSS files. However, defining all logic within a single JavaScript file—such as maintaining global variables—can quickly become challenging, especially if you have many buttons and forms to manage.
React is a framework that enables you to create complex UIs and handle user actions in a much more concise and maintainable way. The downside is that you need to learn and use its conventions and tooling, but in the long run, it's a worthwhile investment. Full documentation is available here.
- Get the latest update from the
upstreamrepository usinggit pull. - Have
npminstalled. You can install it along with Node.js by runningpython -m nodeenv -p --node=20.16.0.npmis a package manager for JavaScript, similar topipfor Python. Node.js is a JavaScript library, mainly used for server-side development. - Run
npm run devto start a development server that automatically bundles your React code.
This will watch your files and rebuild the JavaScript bundle whenever you make changes. Bundling is necessary because React/TypeScript code must be compiled into browser-compatible JavaScript for use in notebooks. - Open a new Terminal window and activate the relevant Conda environment.
- Run
pip install -e .from the project root to install your widget in "editable" mode.
The-eflag ensures that updates to your Python code are immediately available without reinstalling the package. - Open
example.ipynbin the top-level project directory. - Notice that
MyWidgetis defined undersrc/__init__.py. - Notice that
js/awesome_widget.cssandjs/awesome_widget.tsxare automatically rendered tosrc/static/awesome_widget.cssandsrc/static/awesome_widget.js. - To modify or add more styles, edit
js/awesome_widget.css. Restart the Jupyter notebook to see your changes. - Future React code can be introduced in
js/awesome_widget.tsx. Since the current tutorial isn't for React, for more React tutorials, please refer to the below documentation. Also check the FAQs section below.
-
What is
traitlets?- It is a Python library that enables you to create objects with typed attributes that can be synchronized between Python and JavaScript. For example, you can have computed variables in Python available in JavaScript, and vice versa.
-
What is a
trait?- Simply put, it is a powerful "variable" (attribute) that can be synchronized between Python and JavaScript, notifies when its value changes, and can have a default value if needed.
-
What types can be used with
traitlets?traitletssupports common data types likeInt,Float,Unicode,Bool,List,Dict, and more. You can also create custom trait types if needed.
-
What does
model.setdo in the JavaScript code?- It sets a new value for a synced
traiton the frontend model (JavaScript side), which can then be synced back to Python.
- It sets a new value for a synced
-
What does
model.save_changes()do?- It pushes the updated values from the frontend model back to Python, ensuring the backend sees the new values.
-
Can I observe
traitchanges in Python?- Yes. You can use
.observein Python to run a function whenever a trait's value changes.
- Yes. You can use
-
Why do I need to tag a trait with
sync=True?- This tells the system to synchronize that trait between Python and JavaScript.