1- This chapter describes how to enhance your web application by server-side
2- UI-contributions.
1+ The Chartlets framework has two types of users.
32
4- The chartlets backend implementation is provided by the Python package ` chartlets ` .
5- It makes it easy to implement the chartlet endpoints in your preferred
6- webserver framework, such as Flask, FastAPI, or Tornado.
3+ - ** Application contributors ** develop new contributions for a specific web application .
4+ - ** Application providers ** develop the web application and the service that allows
5+ for server-side UI contributions.
76
8- ### 1. Implement the possible contributions
7+
8+ ## Application contributor guide
9+
10+ As a application contributors you develop a Python module that is consumed by
11+ the application's backend service.
12+
13+ Your module is supposed to export one or more instances of the
14+ ` chartlets.Extension ` class. An extension object is a container for your
15+ UI contributions. It groups contributions that logically belong together.
16+
17+ As an example, see [ ` my_extension ` of the demo] ( https://github.com/bcdev/chartlets/tree/main/chartlets.py/my_extension ) .
18+
19+ To develop an extension, follow these steps:
20+
21+ 1 . Create the extension object
22+ 2 . Create the contribution object
23+ 3 . Implement the contribution layout
24+ 4 . Implement the contribution callbacks
25+ 5 . Register the contribution
26+
27+ In the following the above steps are detailed further.
28+
29+ ### Create the extension object
30+
31+ Your contributions to the application are published using a
32+ ` chartlets.Extension ` object that is exported from your extension module.
33+
34+ ``` python
35+ from chartlets import Extension
36+
37+ ext = Extension(" my_dashboard" )
38+ ```
39+
40+ ### Create the contribution object
41+
42+ In a submodule you create a contribution object from an application specific
43+ contribution, e.g., a ` Panel ` . Application-specific contribution classes
44+ are always derived from ` chartlets.Contribution ` .
45+
46+ ``` python
47+ from chartlets.demo import Panel
48+
49+ panel = Panel(title = " Click Statistics" )
50+ ```
51+
52+ ### Implement the contribution layout
53+
54+ In the submodule
55+
56+ ``` python
57+ @panel.layout ()
58+ def get_layout (ctx ):
59+ return Button(id = " button" , text = " Click me" )
60+ ```
61+
62+ ### Implement the contribution callback
63+
64+ In the submodule
65+
66+ ``` python
67+ from chartlets import Import, Output
68+
69+ @panel.callback (
70+ Input(" button" , " n_clicks" ),
71+ Output(" button" , " text" )
72+ )
73+ def on_button_click (ctx , n_clicks ):
74+ n = n_clicks + 1
75+ s = {1 : " st" , 2 : " nd" , 3 : " rd" }.get(n, " th" )
76+ return f " Click me a { n}{ s} time "
77+ ```
78+
79+ ### Register the contribution
80+
81+ In the extension module
82+
83+ ``` python
84+ from chartlets import Extension
85+ from .stats_panel import panel as stats_panel
86+
87+ ext = Extension(" my_dashboard" )
88+ ext.add(stats_panel)
89+ ```
90+
91+ ## Application provider guide
92+
93+ As an application provider you allow for enhancing your web application by
94+ server-side UI-contributions provided by an application contributor.
95+
96+ The Chartlets backend implementation is provided by the Python module
97+ ` chartlets.controllers ` .
98+ It makes it easy to implement the Chartlet endpoints in your preferred
99+ webserver framework, such as Flask, FastAPI, or Tornado.
100+
101+ The following steps are required to enable your web server to support
102+ UI-contributions:
103+
104+ 1 . Implement the possible contributions
105+ 2 . Define the contributions points
106+ 3 . Load the extensions
107+ 4 . Publish the extensions
108+ 5 . Consume the extensions
109+
110+ In the following the above steps are detailed further.
111+
112+ ### Implement the possible contributions
9113
10114Implement the application-specific contributions that users
11115can add to their extensions.
12116
13- As an example, see [ ` panel.py ` of the demo] ( chartlets.py/chartlets/demo/contribs/panel.py ) :
117+ As an example, see [ ` panel.py ` of the demo] ( https://github.com/bcdev/chartlets/tree/main/ chartlets.py/chartlets/demo/contribs/panel.py) :
14118
15119``` python
16120from chartlets import Contribution
@@ -23,11 +127,11 @@ class Panel(Contribution):
23127 super ().__init__ (name, title = title)
24128```
25129
26- ### 2. Define the contributions points
130+ ### Define the contributions points
27131
28132Define the possible contribution points in your application.
29133
30- As an example, see [ ` server.py ` of the demo] ( chartlets.py/chartlets/demo/server.py ) :
134+ As an example, see [ ` server.py ` of the demo] ( https://github.com/bcdev/chartlets/tree/main/ chartlets.py/chartlets/demo/server.py) :
31135
32136``` python
33137from chartlets import Extension
@@ -36,28 +140,28 @@ from chartlets.demo.contribs import Panel
36140Extension.add_contrib_point(" panels" , Panel)
37141```
38142
39- ### 3. Load the extensions
143+ ### Load the extensions
40144
41145Load the extensions that augment your application.
42146
43- As an example, see [ ` server.py ` of the demo] ( chartlets.py/chartlets/demo/server.py ) :
147+ As an example, see [ ` server.py ` of the demo] ( https://github.com/bcdev/chartlets/tree/main/ chartlets.py/chartlets/demo/server.py) :
44148
45149``` python
46150from chartlets import ExtensionContext
47151
48152ext_ctx = ExtensionContext.load(app_ctx, extension_refs)
49153```
50154
51- ### 4. Publish the extensions
155+ ### Publish the extensions
52156
53157Implement the Chartlets API in your application-specific webserver using
54158the controller implementations in ` chartlets.controllers ` .
55159
56- As an example, see [ ` server.py ` of the demo] ( chartlets.py/chartlets/demo/server.py ) .
160+ As an example, see [ ` server.py ` of the demo] ( https://github.com/bcdev/chartlets/tree/main/ chartlets.py/chartlets/demo/server.py) .
57161
58- ### 5. Consume the extensions
162+ ### Consume the extensions
59163
60164Use JavaScript package ` chartlets ` in your frontend to implement the
61165contribution lifecycle in your React application.
62166
63- As an example, see [ the demo application] ( chartlets.js/src/demo ) .
167+ As an example, see [ the demo application] ( https://github.com/bcdev/chartlets/tree/main/ chartlets.js/src/demo) .
0 commit comments