A JupyterLab extension to facilitate invocation of code formatters for multiple programming languages.
—
HTTP client for communication between JupyterLab frontend and Python backend with request handling, error management, and caching support.
Main client class for backend communication.
class JupyterlabCodeFormatterClient {
request(path: string, method: string, body: any): Promise<any>;
getAvailableFormatters(cache: boolean): Promise<string>;
}Methods:
request() - Generic HTTP request method for API communicationgetAvailableFormatters() - Retrieves list of available formatters from backendimport JupyterlabCodeFormatterClient from './client';
// Create client instance
const client = new JupyterlabCodeFormatterClient();// GET request
const response = await client.request('formatters', 'GET', null);
// POST request with body
const formatRequest = {
code: ['def hello(): pass'],
formatter: 'black',
notebook: true,
options: { line_length: 88 }
};
const result = await client.request('format', 'POST', JSON.stringify(formatRequest));// Get formatters without caching
const formattersResponse = await client.getAvailableFormatters(false);
const formatters = JSON.parse(formattersResponse);
console.log(formatters.formatters);
// Output: { black: { enabled: true, label: "Apply Black Formatter" }, ... }
// Get formatters with caching enabled
const cachedResponse = await client.getAvailableFormatters(true);import { ServerConnection } from '@jupyterlab/services';
try {
const result = await client.request('format', 'POST', requestBody);
// Process successful response
} catch (error) {
if (error instanceof ServerConnection.ResponseError) {
console.error('Server error:', error.response.status, error.message);
} else {
console.error('Network error:', error);
}
}The client constructs API URLs using JupyterLab's server settings:
import { URLExt } from '@jupyterlab/coreutils';
import { ServerConnection } from '@jupyterlab/services';
const settings = ServerConnection.makeSettings();
const fullUrl = URLExt.join(
settings.baseUrl,
'jupyterlab_code_formatter', // plugin name
path // API endpoint
);HTTP requests are configured with:
The client processes responses with:
ServerConnection.ResponseError for non-200 statusRetrieves list of available code formatters from backend.
Endpoint: GET /jupyterlab_code_formatter/formatters[?cached]
Query Parameters:
cached (optional) - Use cached formatter availability checksResponse Format:
{
"formatters": {
"black": {
"enabled": true,
"label": "Apply Black Formatter"
},
"isort": {
"enabled": true,
"label": "Apply Isort Formatter"
},
"yapf": {
"enabled": false,
"label": "Apply YAPF Formatter"
}
}
}Sends code to backend for formatting with specified formatter.
Endpoint: POST /jupyterlab_code_formatter/format[?cached]
Request Body:
{
"code": ["def hello():\n pass"],
"formatter": "black",
"notebook": true,
"options": {
"line_length": 88,
"string_normalization": true
}
}Query Parameters:
cached (optional) - Use cached formatter availability checksResponse Format:
{
"code": [
{
"code": "def hello():\n pass\n"
}
]
}Error Response:
{
"code": [
{
"error": "Formatter error message"
}
]
}The client supports caching of formatter availability checks:
?cached query parameter enables cachingimportable property checks// First request - checks actual formatter availability
const initialCheck = await client.getAvailableFormatters(false);
// Subsequent requests - uses cached results
const cachedCheck = await client.getAvailableFormatters(true);
// Format requests can also use caching
const formatResult = await client.request(
'format?cached',
'POST',
JSON.stringify(formatRequest)
);Handles various server connection error scenarios:
try {
const result = await client.request('format', 'POST', requestBody);
return JSON.parse(result);
} catch (error) {
if (error instanceof ServerConnection.ResponseError) {
// Handle HTTP errors
switch (error.response.status) {
case 404:
console.error('Formatter not found');
break;
case 500:
console.error('Server error during formatting');
break;
default:
console.error('HTTP error:', error.response.status);
}
} else {
// Handle network errors
console.error('Network error:', error.message);
}
throw error;
}The HTTP client coordinates between frontend formatters and backend services:
The client passes configuration from frontend to backend:
Install with Tessl CLI
npx tessl i tessl/pypi-jupyterlab-code-formatter