|
| 1 | +"""Utilities for configuring JupyterHub services.""" |
| 2 | +from typing import Dict, Any, Optional |
| 3 | + |
| 4 | +from jhub_apps.service.models import AdditionalService |
| 5 | + |
| 6 | + |
| 7 | +def service_for_jhub_apps( |
| 8 | + name: str, |
| 9 | + url: str, |
| 10 | + description: Optional[str] = None, |
| 11 | + pinned: bool = False, |
| 12 | + thumbnail: Optional[str] = None |
| 13 | +) -> Dict[str, Any]: |
| 14 | + """Create a service configuration dict for JupyterHub services. |
| 15 | +
|
| 16 | + This helper function creates the proper structure for external services |
| 17 | + that appear in the JupyterHub UI services menu. Services with pinned=True |
| 18 | + also appear in the quick access section. It validates the input using |
| 19 | + the AdditionalService Pydantic model. |
| 20 | +
|
| 21 | + Args: |
| 22 | + name: Display name of the service |
| 23 | + url: URL path for the service |
| 24 | + description: Optional description of the service |
| 25 | + pinned: Whether the service should appear in the quick access section |
| 26 | + thumbnail: Optional thumbnail URL or base64-encoded data URL for the service icon |
| 27 | +
|
| 28 | + Returns: |
| 29 | + Dictionary with JupyterHub service configuration |
| 30 | +
|
| 31 | + Raises: |
| 32 | + ValidationError: If the input parameters don't pass Pydantic validation |
| 33 | +
|
| 34 | + Example: |
| 35 | + >>> from jhub_apps import service_for_jhub_apps |
| 36 | + >>> service = service_for_jhub_apps( |
| 37 | + ... name="Monitoring", |
| 38 | + ... url="/grafana", |
| 39 | + ... pinned=True, |
| 40 | + ... description="System monitoring dashboard" |
| 41 | + ... ) |
| 42 | + """ |
| 43 | + # Validate inputs using Pydantic model |
| 44 | + additional_service = AdditionalService( |
| 45 | + name=name, |
| 46 | + url=url, |
| 47 | + description=description, |
| 48 | + pinned=pinned, |
| 49 | + thumbnail=thumbnail, |
| 50 | + ) |
| 51 | + |
| 52 | + return { |
| 53 | + "name": additional_service.name, |
| 54 | + "display": True, |
| 55 | + "info": { |
| 56 | + "name": additional_service.name, |
| 57 | + "description": additional_service.description, |
| 58 | + "url": additional_service.url, |
| 59 | + "external": True, |
| 60 | + "pinned": additional_service.pinned, |
| 61 | + "thumbnail": additional_service.thumbnail, |
| 62 | + }, |
| 63 | + } |
| 64 | + |
| 65 | + |
| 66 | +def additional_service_to_service_dict(additional_service: AdditionalService) -> Dict[str, Any]: |
| 67 | + """Convert an AdditionalService model to a JupyterHub service dict. |
| 68 | +
|
| 69 | + Args: |
| 70 | + additional_service: AdditionalService model instance |
| 71 | +
|
| 72 | + Returns: |
| 73 | + Dictionary with JupyterHub service configuration |
| 74 | + """ |
| 75 | + return service_for_jhub_apps( |
| 76 | + name=additional_service.name, |
| 77 | + url=additional_service.url, |
| 78 | + description=additional_service.description, |
| 79 | + pinned=additional_service.pinned, |
| 80 | + thumbnail=additional_service.thumbnail, |
| 81 | + ) |
0 commit comments