-
Notifications
You must be signed in to change notification settings - Fork 21
Add concept of block events and some design notes #1059
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ml-evs
wants to merge
19
commits into
main
Choose a base branch
from
ml-evs/blocks
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
c4396eb
Add concept of block events
ml-evs be61a54
Add design document for data blocks
ml-evs c481d62
Add event handling to base Vue data block
ml-evs 98b48de
Expand design doc to include example of `CustomEvent` inside bokeh
ml-evs 15d49a5
Add simple base block event test
ml-evs e7b04bc
Add validation and test for `set_wavelength` event
ml-evs d393f8b
Add basic error handling to block events
ml-evs 35b43eb
Call the block events inside the update-block route
ml-evs 1e8dce8
Do not pass event name directly to event
ml-evs cdebfb7
Do not raise from process events but log errors
ml-evs 8d2e44c
Add missing dispatch event to docs
ml-evs 45da4ef
Actualy send event data to block from UI
ml-evs 1b83b2d
Add ref to base data block div
ml-evs ad9c812
Use `set_wavelength` event from within XRD block
ml-evs fde4f48
Try to use block refs
ml-evs 3f6c7e2
Stub test for event
ml-evs c2fe329
Try different approach to add event listener to ref
ml-evs ef3ca10
Directly call event handler rather than dispatching event for XRD block
ml-evs a3a5d03
WIP: add Bokeh input boxes with events parametrized by Python code
ml-evs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
# Data blocks | ||
|
||
## Overview | ||
|
||
*datalab*'s block system provides a modular approach to data processing and visualization. | ||
Each block type is a specialized component that handles specific kinds of data and operations, making it easy to extend the system's capabilities without modifying the core architecture. | ||
Typically, a given technique (e.g., XRD, NMR) will have its own block. | ||
Blocks can be implemented either in the main package, or as a plugin (see ["Plugin Development"](design/plugins.md)). | ||
|
||
Data blocks are modular components that: | ||
|
||
1. Process specific file types and data formats for a technique or set of techniques, | ||
2. Generate visualizations and plots from this data to be shown in the UI, | ||
3. Store and manage their own state persistently in a database, | ||
4. Can be attached to individual items or collections in your data management system, | ||
5. Provide a mechanism for handling "events" through a decorator-based registration system, | ||
6. Expose a consistent API for creation, updating, and deletion. | ||
7. Handle logging, errors and warnings in a consistent way to show in the UI. | ||
|
||
## Block Lifecycle | ||
|
||
1. **Creation**: Blocks are instantiated with an item or collection ID | ||
2. **Initialization**: Initial state is set up, potentially including file data and defaults | ||
3. **Processing**: Data is processed, plots are generated, and state is updated | ||
4. **Serialization**: Block state is serialized for storage or transmission | ||
5. **Update**: Blocks can receive updates from the web interface | ||
6. **Deletion**: Blocks can be removed from items or collections | ||
|
||
|
||
## Web API | ||
|
||
The block system exposes several API endpoints: | ||
|
||
- `/add-data-block/`: Create and add a new block to an item | ||
- `/add-collection-data-block/`: Create and add a new block to a collection | ||
- `/update-block/`: Update an existing block's state | ||
- `/delete-block/`: Remove a block from an item | ||
- `/delete-collection-block/`: Remove a block from a collection | ||
|
||
## Creating a new block | ||
|
||
To create a new block type: | ||
|
||
1. Create a class that inherits from `DataBlock` | ||
2. Define the accepted file extensions and block metadata (descriptions will be used to populate the UI documentation automatically)- | ||
3. Implement data processing and visualization methods, with e.g., JSON-serialized Bokeh plots stored in the `self.data["bokeh_plot_data"]` attribute | ||
4. Any data to be stored in the database can be defined in the `self.data` attribute | ||
5. Register any event handlers using the `@event` decorator | ||
5. Add the block type to the `BLOCK_TYPES` registry | ||
|
||
By default, a generic UI component will be used in the *datalab* interface that | ||
will make use of titles, descriptions, accepted file extensions to render a | ||
simple user interface for the block. | ||
When the user loads the block in the UI, the block's `plot_functions` methods | ||
will be called in turn, which will either load from scratch, or load cached data | ||
for that block. | ||
If a JSON-serialized Bokeh plot is found in the block's data, this will be | ||
rendered in the UI. | ||
|
||
## Event system | ||
|
||
The event system allows external functions to be called by name, enabling clean interaction between the frontend and server-side block functionality. | ||
This is a new feature and this documentation will evolve alongside it. | ||
|
||
Currently, the event system allows: | ||
|
||
- Registration of event handlers in Python via the `@event` decorator | ||
- Access to available events at both class and instance levels | ||
- Runtime dispatch of events based on name | ||
- Support for event parameters passed as keyword arguments | ||
- Events can then be triggered by the front-end; for example, a Bokeh-based block can trigger an event in a Bokeh callback using the [`CustomEvent`](https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent/CustomEvent) API, for example: | ||
```javascript | ||
const event = new CustomEvent("bokehStateUpdate", { | ||
detail: { | ||
event_name: '<event_name>', | ||
state_data: '<some data>', | ||
}, | ||
bubbles: true | ||
}); | ||
document.dispatchEvent(event); | ||
``` | ||
The base data block (`DataBlockBase.vue`) will listen for such events registered as `'bokehStateUpdate'` and pass them to the appropriate server-side block. | ||
|
||
|
||
## Future Directions | ||
|
||
Future updates to the block system will focus on: | ||
|
||
- Reducing boilerplate code required for new block types | ||
- Enhanced automatic caching after block creation | ||
- Improving the event system to enable richer UI interactions, e.g,. setting user parameters or controlling default plot styles. | ||
- Providing better support for custom user interfaces (i.e., allowing plugins to also specify custom Vue code). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you show how this gets implemented in the XRD block before we merge? That would be a good way to test that everything is working on the front end