|
| 1 | +# Providers guide |
| 2 | + |
| 3 | +As an application provider you allow for enhancing your web application by |
| 4 | +server-side UI-contributions provided by an application contributor. |
| 5 | + |
| 6 | +## How Chartlets works |
| 7 | + |
| 8 | +Users write the widgets in, e.g. Python, and a REST server implements three |
| 9 | +endpoints to publish the widgets: |
| 10 | + |
| 11 | +- `GET /contributions`: Called once after application UI starts up. |
| 12 | + Returns an object whose keys are contribution points (e.g., "panels") |
| 13 | + and whose values are arrays of contribution objects. |
| 14 | +- `POST /layout/{contribPoint}/{contribIndex}`: |
| 15 | + Called once for every contribution when it becomes visible in the UI. |
| 16 | + Returns the contribution's initial component tree. |
| 17 | +- `POST /callback`: |
| 18 | + Called when users interact with the component tree or on application |
| 19 | + state changes. Returns an array of contribution changes where each |
| 20 | + contribution change contains an array of actions to be applied to the |
| 21 | + component tree. |
| 22 | + |
| 23 | +The following sequence diagram depicts how the library is supposed to |
| 24 | +work. The top shows the JavaScript frontend that uses this library. |
| 25 | +The bottom shows the lifeline of the backend REST server. |
| 26 | + |
| 27 | + |
| 28 | + |
| 29 | +## Backend integration |
| 30 | + |
| 31 | +The Chartlets backend implementation is provided by the module |
| 32 | +`chartlets.controllers` of the Python package `chartlets`. |
| 33 | +It makes it easy to integrate the Chartlet endpoints in your preferred |
| 34 | +webserver framework, such as Flask, FastAPI, or Tornado. |
| 35 | + |
| 36 | +The following steps are required to enable your web server to support |
| 37 | +UI-contributions: |
| 38 | + |
| 39 | +1. Update project dependencies |
| 40 | +2. Implement the possible contributions |
| 41 | +3. Define the contributions points |
| 42 | +4. Load the extensions |
| 43 | +5. Publish the extensions |
| 44 | + |
| 45 | +In the following the above steps are detailed further. |
| 46 | + |
| 47 | +### Update project dependencies |
| 48 | + |
| 49 | +Add the Python package `chartlets` to your project dependencies. |
| 50 | +Currently, Chartlets is available from PyPI only. |
| 51 | + |
| 52 | +### Implement the possible contributions |
| 53 | + |
| 54 | +Implement the application-specific contributions that users |
| 55 | +can add to their extensions. |
| 56 | + |
| 57 | +As an example, see [`panel.py` of the demo](https://github.com/bcdev/chartlets/tree/main/chartlets.py/chartlets/demo/contribs/panel.py): |
| 58 | + |
| 59 | +```python |
| 60 | +from chartlets import Contribution |
| 61 | + |
| 62 | + |
| 63 | +class Panel(Contribution): |
| 64 | + """Panel contribution""" |
| 65 | + |
| 66 | + def __init__(self, name: str, title: str | None = None): |
| 67 | + super().__init__(name, title=title) |
| 68 | +``` |
| 69 | + |
| 70 | +### Define the contributions points |
| 71 | + |
| 72 | +Define the possible contribution points in your application. |
| 73 | + |
| 74 | +As an example, see [`server.py` of the demo](https://github.com/bcdev/chartlets/tree/main/chartlets.py/chartlets/demo/server.py): |
| 75 | + |
| 76 | +```python |
| 77 | +from chartlets import Extension |
| 78 | +from chartlets.demo.contribs import Panel |
| 79 | + |
| 80 | +Extension.add_contrib_point("panels", Panel) |
| 81 | +``` |
| 82 | + |
| 83 | +### Load the extensions |
| 84 | + |
| 85 | +Load the extensions that augment your application. |
| 86 | + |
| 87 | +As an example, see [`server.py` of the demo](https://github.com/bcdev/chartlets/tree/main/chartlets.py/chartlets/demo/server.py): |
| 88 | + |
| 89 | +```python |
| 90 | +from chartlets import ExtensionContext |
| 91 | + |
| 92 | +ext_ctx = ExtensionContext.load(app_ctx, extension_refs) |
| 93 | +``` |
| 94 | + |
| 95 | +### Publish the extensions |
| 96 | + |
| 97 | +Implement the Chartlets API in your application-specific webserver using |
| 98 | +the controller implementations in `chartlets.controllers`. |
| 99 | + |
| 100 | +As an example, see [`server.py` of the demo](https://github.com/bcdev/chartlets/tree/main/chartlets.py/chartlets/demo/server.py). |
| 101 | + |
| 102 | +## Frontend integration |
| 103 | + |
| 104 | +The JavaScript package `chartlets` provides the types, actions, and hooks |
| 105 | +to allow for supporting server-side UI contributions in your React |
| 106 | +application. |
| 107 | + |
| 108 | +As an example, see [the demo application](https://github.com/bcdev/chartlets/tree/main/chartlets.js/src/demo). |
| 109 | + |
| 110 | +As an application provider you will need to perform the |
| 111 | +following steps: |
| 112 | + |
| 113 | +1. Update project dependencies |
| 114 | +2. Configure the framework |
| 115 | +3. Implement derived application state |
| 116 | +4. Render the contributions |
| 117 | + |
| 118 | +### Update project dependencies |
| 119 | + |
| 120 | +Add the `chartlets` package as a dependency to your `package.json`. |
| 121 | +The package provides also TypeScript type definitions. |
| 122 | +There is nothing more to be considered. |
| 123 | + |
| 124 | +### Configure the framework |
| 125 | + |
| 126 | +Before the framework can be used it must configured |
| 127 | +using the `initializeFramework` function. |
| 128 | + |
| 129 | +```TypeScript |
| 130 | +import { initializeFramework } "charlets" |
| 131 | +``` |
| 132 | +
|
| 133 | +### Implement derived application state |
| 134 | +
|
| 135 | +### Render the contributions |
| 136 | +
|
| 137 | +## Adding new components |
0 commit comments