Skip to content

config

plantimager.webui.config Link

PlantDB Configuration Interface for Plant Imager Web UI.

This module provides components and callbacks for configuring the connection to the PlantDB REST API and managing datasets within the Plant Imager web interface.

Key Features
  • REST API connection configuration and testing
  • Dataset listing and management
  • Dynamic UI components for database status visualization
  • Bootstrap-styled modals and alerts for user interaction

check_server_availability Link

check_server_availability(_, host, port, prefix, stored_host, stored_port, stored_prefix)

Checks the availability of a server based on the provided host and port and updates the UI accordingly.

Parameters:

Name Type Description Default
_ int

The n_clicks property of the load-plantdb-button element, which triggers the callback (not used).

required
host str

The IP address or hostname of the server to test.

required
port int

The port number of the server to test.

required
stored_host str

The previously stored server host value, used if the connection test fails.

required
stored_port int

The previously stored server port value, used if the connection test fails.

required

Returns:

Type Description
bool

A storer flag indicating whether the connection test was successful.

bool

A boolean indicating whether the load button should be disabled.

str

The updated server host value to store.

int

The updated server port value to store.

Source code in plantimager/webui/config.py
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
@callback(
    Output('connected', 'data'),
    Output('load-plantdb-button', 'disabled'),
    Output('rest-api-host', 'data'),
    Output('rest-api-port', 'data'),
    Output('rest-api-prefix', 'data'),
    Input('connect-plantdb-button', 'n_clicks'),
    State('api-address', 'value'),
    State('api-port', 'value'),
    State('api-prefix', 'value'),
    State('rest-api-host', 'data'),
    State('rest-api-port', 'data'),
    State('rest-api-prefix', 'data'),
)
def check_server_availability(
        _: int,
        host: str | None,
        port: int | str | None,
        prefix: str | None,
        stored_host: str | None,
        stored_port: int | str | None,
        stored_prefix: str | None,
) -> tuple[bool, bool, str, int | str]:
    """Checks the availability of a server based on the provided host and port and updates the UI accordingly.

    Parameters
    ----------
    _ : int
        The n_clicks property of the load-plantdb-button element, which triggers the callback (not used).
    host : str
        The IP address or hostname of the server to test.
    port : int
        The port number of the server to test.
    stored_host : str
        The previously stored server host value, used if the connection test fails.
    stored_port : int
        The previously stored server port value, used if the connection test fails.

    Returns
    -------
    bool
        A storer flag indicating whether the connection test was successful.
    bool
        A boolean indicating whether the load button should be disabled.
    str
        The updated server host value to store.
    int
        The updated server port value to store.
    """
    if host is None:
        host = stored_host
    if port is None:
        port = stored_port
    if prefix is None:
        prefix = stored_prefix
    if host.startswith("http://"):
        host = host[7:]
    elif host.startswith("https://"):
        host = host[8:]

    try:
        test_availability({base_url(host, port, prefix)})
    except:
        is_available = False
    else:
        is_available = True

    if is_available:
        return True, False, host, port, prefix
    else:
        return False, True, host, port, prefix

create_dataset_cfg_icon Link

create_dataset_cfg_icon(is_connected=False, dataset_list=None)

Create a navigation link with the database status icon and dataset counter badge.

Creates a Bootstrap NavLink component that displays a database status icon and a badge showing the number of datasets. The icon changes appearance based on connection status.

Parameters:

Name Type Description Default
is_connected bool

Flag indicating whether the database connection is established, by default False

False
dataset_list list

List of datasets to count, by default empty list

None

Returns:

Type Description
NavLink

A navigation link component containing: - Database status icon - Badge showing dataset count The NavLink is styled and positioned according to the application's design.

Source code in plantimager/webui/config.py
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
def create_dataset_cfg_icon(is_connected: bool = False, dataset_list: list | None = None) -> dbc.NavLink:
    """Create a navigation link with the database status icon and dataset counter badge.

    Creates a Bootstrap NavLink component that displays a database status icon and
    a badge showing the number of datasets. The icon changes appearance based on
    connection status.

    Parameters
    ----------
    is_connected : bool, optional
        Flag indicating whether the database connection is established,
        by default False
    dataset_list : list, optional
        List of datasets to count, by default empty list

    Returns
    -------
    dash.bootstrap_components.NavLink
        A navigation link component containing:
        - Database status icon
        - Badge showing dataset count
        The NavLink is styled and positioned according to the application's design.
    """
    if dataset_list is None:
        dataset_list = []
    return dbc.NavLink(
        children=[
            dataset_cfg_status(is_connected),
            dbc.Badge(
                children=f"{len(dataset_list)}",
                id="dataset-count-badge",
                color="primary",
                className="position-absolute top-45 start-100 translate-middle",
                pill=True
            )
        ],
        id='plantdb-cfg-button',
        n_clicks=0,
        className="position-relative align-left",
        style={'color': "#f3f3f3"},
    )

dataset_cfg_status Link

dataset_cfg_status(is_connected)

Generate a database status icon based on the connection state.

Creates a Bootstrap icon element representing the database connection status. Returns a check icon when connected and a gear icon when disconnected.

Parameters:

Name Type Description Default
is_connected bool

Flag indicating whether the database connection is established.

required

Returns:

Type Description
I

A Bootstrap icon component with the appropriate class based on connection status.

Source code in plantimager/webui/config.py
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
def dataset_cfg_status(is_connected: bool) -> html.I:
    """Generate a database status icon based on the connection state.

    Creates a Bootstrap icon element representing the database connection status.
    Returns a check icon when connected and a gear icon when disconnected.

    Parameters
    ----------
    is_connected : bool
        Flag indicating whether the database connection is established.

    Returns
    -------
    dash.html.I
        A Bootstrap icon component with the appropriate class based on connection status.
    """
    if is_connected:
        return html.I(className="bi bi-database-check fs-3")
    else:
        return html.I(className="bi bi-database-gear fs-3")

show_api_address Link

show_api_address(modal_is_open, stored_host)

Callback updating the value of the 'api-address' field with stored data when opening the PlantDB configuration modal.

Parameters:

Name Type Description Default
modal_is_open bool

Indicates whether the configuration modal is currently open.

required
stored_host str or None

The stored value of the REST API host. Can be None if not previously set.

required

Returns:

Type Description
str

The IP address to be used, either the stored host value or the REST_API_URL constant.

Source code in plantimager/webui/config.py
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
@callback(
    Output("api-address", "value"),
    Input("plantdb-cfg-modal", "is_open"),
    State("rest-api-host", "data")
)
def show_api_address(modal_is_open: bool, stored_host: str | None) -> str:
    """Callback updating the value of the 'api-address' field with stored data when opening the PlantDB configuration modal.

    Parameters
    ----------
    modal_is_open : bool
        Indicates whether the configuration modal is currently open.
    stored_host : str or None
        The stored value of the REST API host. Can be ``None`` if not previously set.

    Returns
    -------
    str
        The IP address to be used, either the stored host value or the ``REST_API_URL`` constant.
    """
    if modal_is_open:
        return stored_host if stored_host is not None else REST_API_URL
    else:
        return stored_host

show_api_port Link

show_api_port(modal_is_open, stored_port)

Callback updating the value of the 'api-port' field with stored data when opening the PlantDB configuration modal.

Parameters:

Name Type Description Default
modal_is_open bool

Boolean indicating whether the configuration modal is open or closed.

required
stored_port str or None

Stored port value retrieved from the state. Can be None if not specified.

required

Returns:

Type Description
str

Value to be set for the "api-port" input field, either the stored port value or the REST_API_PORT constant.

Source code in plantimager/webui/config.py
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
@callback(
    Output("api-port", "value"),
    Input("plantdb-cfg-modal", "is_open"),
    State("rest-api-port", "data")
)
def show_api_port(modal_is_open: bool, stored_port: int | None) -> int | str:
    """Callback updating the value of the 'api-port' field with stored data when opening the PlantDB configuration modal.

    Parameters
    ----------
    modal_is_open : bool
        Boolean indicating whether the configuration modal is open or closed.
    stored_port : str or None
        Stored port value retrieved from the state. Can be ``None`` if not specified.

    Returns
    -------
    str
        Value to be set for the "api-port" input field, either the stored port value or the ``REST_API_PORT`` constant.
    """
    if modal_is_open:
        return stored_port if stored_port is not None else REST_API_PORT
    else:
        return stored_port

show_api_prefix Link

show_api_prefix(modal_is_open, stored_prefix)

Callback updating the value of the 'api-prefix' field with stored data when opening the PlantDB configuration modal.

Parameters:

Name Type Description Default
modal_is_open bool

Boolean indicating whether the configuration modal is open or closed.

required
stored_prefix str or None

Stored prefix value retrieved from the state. Can be None if not specified.

required

Returns:

Type Description
str

Value to be set for the "api-prefix" input field.

Source code in plantimager/webui/config.py
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
@callback(
    Output("api-prefix", "value"),
    Input("plantdb-cfg-modal", "is_open"),
    State("rest-api-prefix", "data")
)
def show_api_prefix(modal_is_open: bool, stored_prefix: str | None) -> int | str:
    """Callback updating the value of the 'api-prefix' field with stored data when opening the PlantDB configuration modal.

    Parameters
    ----------
    modal_is_open : bool
        Boolean indicating whether the configuration modal is open or closed.
    stored_prefix : str or None
        Stored prefix value retrieved from the state. Can be ``None`` if not specified.

    Returns
    -------
    str
        Value to be set for the "api-prefix" input field.
    """
    if modal_is_open:
        return stored_prefix if stored_prefix is not None else ""
    else:
        return stored_prefix

show_plantdb_status Link

show_plantdb_status(status, host, port, prefix)

Display the connection status of the PlantDB server in a Bootstrap alert component.

This callback function generates a styled alert component that shows whether the PlantDB server is available or not. The alert includes an icon and descriptive text with the server's host and port information when applicable.

Parameters:

Name Type Description Default
status bool or None

Connection status of the PlantDB server: - None: status unknown - True: server is available - False: server is unavailable

required
host str

Hostname or IP address of the PlantDB server

required
port int

Port number of the PlantDB server

required
prefix str

URL prefix of the PlantDB server

required

Returns:

Type Description
Alert

A Bootstrap alert component with the appropriate styling and message based on the connection status: - Info (blue): when status is unknown - Success (green): when server is available - Danger (red): when server is unavailable

Source code in plantimager/webui/config.py
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
@callback(
    Output("plantdb-status-form", "children"),
    Input("connected", "data"),
    State("rest-api-host", "data"),
    State("rest-api-port", "data"),
    State("rest-api-prefix", "data"),
)
def show_plantdb_status(status: bool | None, host: str, port: int, prefix: str) -> dbc.Alert:
    """Display the connection status of the PlantDB server in a Bootstrap alert component.

    This callback function generates a styled alert component that shows whether the
    PlantDB server is available or not. The alert includes an icon and descriptive text
    with the server's host and port information when applicable.

    Parameters
    ----------
    status : bool or None
        Connection status of the PlantDB server:
        - ``None``: status unknown
        - ``True``: server is available
        - ``False``: server is unavailable
    host : str
        Hostname or IP address of the PlantDB server
    port : int
        Port number of the PlantDB server
    prefix : str
        URL prefix of the PlantDB server

    Returns
    -------
    dash_bootstrap_components.Alert
        A Bootstrap alert component with the appropriate styling and message based on
        the connection status:
        - Info (blue): when status is unknown
        - Success (green): when server is available
        - Danger (red): when server is unavailable
    """
    if status is None:
        status_form = dbc.Alert(children=[
            html.I(className="bi bi-info-circle-fill me-2"),
            "Unknown server availability."
        ], color="info")
    elif status:
        status_form = dbc.Alert(children=[
            html.I(className="bi bi-check-circle-fill me-2"), f"Server {base_url(host, port, prefix)} available.",
        ], color="success")
    else:
        status_form = dbc.Alert(children=[
            html.I(className="bi bi-x-octagon-fill me-2"), f"Server {base_url(host, port, prefix)} unavailable!",
        ], color="danger")
    return status_form

toggle_plantdb_cfg_modal Link

toggle_plantdb_cfg_modal(n_clicks, is_open)

Toggle the visibility state of the PlantDB configuration modal.

This callback function controls the opening and closing of the PlantDB configuration modal dialog. It is triggered by clicking on the configuration button and toggles the modal's visibility state.

Parameters:

Name Type Description Default
n_clicks int

Number of times the plantdb-cfg-button has been clicked. Used to determine when to toggle the modal state.

required
is_open bool

Current visibility state of the modal.

required

Returns:

Type Description
bool or None

The new visibility state of the modal. Returns the opposite of the current state when n_clicks > 0, otherwise returns None.

Source code in plantimager/webui/config.py
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
@callback(Output("plantdb-cfg-modal", "is_open"),
          Input('plantdb-cfg-button', 'n_clicks'),
          State('plantdb-cfg-modal', 'is_open'),
          prevent_initial_call=True)
def toggle_plantdb_cfg_modal(n_clicks: int, is_open: bool) -> bool | None:
    """Toggle the visibility state of the PlantDB configuration modal.

    This callback function controls the opening and closing of the PlantDB configuration
    modal dialog. It is triggered by clicking on the configuration button and toggles
    the modal's visibility state.

    Parameters
    ----------
    n_clicks : int
        Number of times the plantdb-cfg-button has been clicked. Used to determine
        when to toggle the modal state.
    is_open : bool
        Current visibility state of the modal.

    Returns
    -------
    bool or None
        The new visibility state of the modal. Returns the opposite of the current
        state when n_clicks > 0, otherwise returns None.
    """
    if n_clicks > 0:
        return not is_open

update_dataset_badge Link

update_dataset_badge(dataset_list)

Update the dataset count badge with the current number of datasets.

This callback function updates a badge element in the UI to display the total number of datasets currently available in the system. It converts the length of the dataset list to a string for display.

Parameters:

Name Type Description Default
dataset_list list

A list containing the dataset names stored in the dcc.Store component.

required

Returns:

Type Description
str

String representation of the number of datasets in the list.

Source code in plantimager/webui/config.py
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
@callback(
    Output("dataset-count-badge", "children"),
    Input("dataset-list", "data")  # Assuming you have a dataset list stored in dcc.Store
)
def update_dataset_badge(dataset_list: list) -> str:
    """Update the dataset count badge with the current number of datasets.

    This callback function updates a badge element in the UI to display the total
    number of datasets currently available in the system. It converts the length
    of the dataset list to a string for display.

    Parameters
    ----------
    dataset_list : list
        A list containing the dataset names stored in the dcc.Store component.

    Returns
    -------
    str
        String representation of the number of datasets in the list.
    """
    return str(len(dataset_list))

update_dataset_list Link

update_dataset_list(n_clicks, connected, host, port, prefix)

Callback updating the dataset list and displaying the load status when a user clicks the load button.

Parameters:

Name Type Description Default
n_clicks int

The n_clicks property of the load-plantdb-button element, which triggers the callback (not used).

required
connected bool

The connection status of the PlantDB REST API server.

required
host str

The hostname or IP address of the PlantDB REST API server.

required
port int

The port number of the PlantDB REST API server.

required
prefix str

The prefix of the PlantDB REST API server.

required

Returns:

Type Description
Alert

An alert component indicating success or failure of dataset retrieval.

list of str

A list of dataset names retrieved from the server.

Source code in plantimager/webui/config.py
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
@callback(
    Output('load-status-form', 'children'),
    Output('dataset-list', 'data'),
    Input('load-plantdb-button', 'n_clicks'),
    Input('connected', 'data'),
    State('rest-api-host', 'data'),
    State('rest-api-port', 'data'),
    State('rest-api-prefix', 'data'),
    prevent_initial_call=True,
)
def update_dataset_list(
        n_clicks: int,
        connected: bool | None,
        host: str,
        port: int | str,
        prefix: str,
) -> tuple[dbc.Alert, list[str]]:
    """Callback updating the dataset list and displaying the load status when a user clicks the load button.

    Parameters
    ----------
    n_clicks : int
        The n_clicks property of the load-plantdb-button element, which triggers the callback (not used).
    connected : bool
        The connection status of the PlantDB REST API server.
    host : str
       The hostname or IP address of the PlantDB REST API server.
    port : int
        The port number of the PlantDB REST API server.
    prefix : str
        The prefix of the PlantDB REST API server.

    Returns
    -------
    dash_bootstrap_components.Alert
        An alert component indicating success or failure of dataset retrieval.
    list of str
        A list of dataset names retrieved from the server.
    """
    if not connected:
        return _unconnected_status(), []

    try:
        dataset_list = list_scan_names(host=host, port=port, prefix=prefix)
    except RequestException:
        dataset_list = []

    if dataset_list:
        status = _connected_status(dataset_list)
    else:
        status = _unconnected_status()

    return status, dataset_list

update_plantdb_cfg_button Link

update_plantdb_cfg_button(status, dataset_list)

Update the PlantDB configuration button's appearance based on connection status.

This callback function updates the visual representation of the PlantDB configuration button in the navigation bar depending on the connection status and dataset list state. It uses the create_dataset_cfg_icon function to generate the appropriate icon.

Parameters:

Name Type Description Default
status bool or None

The connection status to the PlantDB REST API. None indicates no connection attempt has been made. True indicates a successful connection. False indicates a failed connection.

required
dataset_list list or None

List of available datasets from the PlantDB. None if no datasets are loaded or connection is not established.

required

Returns:

Type Description
Component

A Dash HTML component representing the configuration button icon with the appropriate styling based on the connection status.

Source code in plantimager/webui/config.py
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
@callback(
    Output("plantdb-cfg-button", "children"),
    Input("connected", "data"),
    State("dataset-list", "data"),
)
def update_plantdb_cfg_button(status: bool | None, dataset_list: list | None) -> list:
    """Update the PlantDB configuration button's appearance based on connection status.

    This callback function updates the visual representation of the PlantDB configuration
    button in the navigation bar depending on the connection status and dataset list state.
    It uses the create_dataset_cfg_icon function to generate the appropriate icon.

    Parameters
    ----------
    status : bool or None
        The connection status to the PlantDB REST API.
        ``None`` indicates no connection attempt has been made.
        ``True`` indicates a successful connection.
        ``False`` indicates a failed connection.
    dataset_list : list or None
        List of available datasets from the PlantDB.
        ``None`` if no datasets are loaded or connection is not established.

    Returns
    -------
    dash.html.Component
        A Dash HTML component representing the configuration button icon
        with the appropriate styling based on the connection status.
    """
    return create_dataset_cfg_icon(status, dataset_list)