A JupyterLab extension to facilitate invocation of code formatters for multiple programming languages.
npx @tessl/cli install tessl/pypi-jupyterlab-code-formatter@3.0.0A comprehensive JupyterLab extension that provides code formatting capabilities for multiple programming languages including Python, R, Scala, Rust, and C/C++. The extension integrates seamlessly with JupyterLab's interface, offering both notebook cell formatting and file editor formatting with support for various formatters like Black, isort, YAPF, Autopep8, Ruff, and more.
pip install jupyterlab-code-formatterfrom jupyterlab_code_formatter import _load_jupyter_server_extension
from jupyterlab_code_formatter.formatters import SERVER_FORMATTERS
from jupyterlab_code_formatter.handlers import setup_handlersimport { JupyterFrontEndPlugin } from '@jupyterlab/application';
import JupyterlabCodeFormatterClient from './client';
import { JupyterlabNotebookCodeFormatter, JupyterlabFileEditorCodeFormatter } from './formatter';# Install the extension
pip install jupyterlab-code-formatter
# Install formatters (example for Python)
pip install black isort
# Restart JupyterLabOnce installed, the extension provides:
from jupyterlab_code_formatter.formatters import BlackFormatter
# Create formatter instance
formatter = BlackFormatter()
# Check if formatter is available
if formatter.importable:
# Format code
formatted_code = formatter.format_code(
code="def hello():pass",
notebook=True,
line_length=88
)JupyterLab Code Formatter follows a client-server architecture:
Key components:
JupyterLab user interface integration including toolbar buttons, menu items, command palette integration, and format-on-save functionality.
class JupyterLabCodeFormatter implements DocumentRegistry.IWidgetExtension<NotebookPanel, INotebookModel> {
constructor(
app: JupyterFrontEnd,
tracker: INotebookTracker,
palette: ICommandPalette,
settingRegistry: ISettingRegistry,
menu: IMainMenu,
editorTracker: IEditorTracker
);
createNew(nb: NotebookPanel, context: DocumentRegistry.IContext<INotebookModel>): IDisposable;
private setupSettings(): Promise<void>;
private setupAllCommands(): void;
private onSave(context: DocumentRegistry.IContext<INotebookModel>, state: DocumentRegistry.SaveState): Promise<void>;
private createNewEditor(widget: DocumentWidget, context: DocumentRegistry.IContext<DocumentModel>): IDisposable;
private onSaveEditor(context: DocumentRegistry.IContext<DocumentModel>, state: DocumentRegistry.SaveState): Promise<void>;
private setupWidgetExtension(): void;
private setupContextMenu(): void;
private setupCommand(name: string, label: string, command: string): void;
}Comprehensive notebook cell formatting capabilities including selected cells, all cells, and format-on-save functionality with support for magic commands and special syntax.
class JupyterlabNotebookCodeFormatter {
constructor(client: JupyterlabCodeFormatterClient, notebookTracker: INotebookTracker);
formatAction(config: any, formatter?: string): Promise<void>;
formatSelectedCodeCells(config: any, formatter?: string, notebook?: Notebook): Promise<void>;
formatAllCodeCells(config: any, context: Context, formatter?: string, notebook?: Notebook): Promise<void>;
applicable(formatter: string, currentWidget: Widget): boolean;
}File editor formatting capabilities for standalone code files with language detection and format-on-save support.
class JupyterlabFileEditorCodeFormatter {
constructor(client: JupyterlabCodeFormatterClient, editorTracker: IEditorTracker);
formatAction(config: any, formatter: string): Promise<void>;
formatEditor(config: any, context: Context, formatter?: string): Promise<void>;
applicable(formatter: string, currentWidget: Widget): boolean;
}HTTP client for communication between frontend and backend with request handling and error management.
class JupyterlabCodeFormatterClient {
request(path: string, method: string, body: any): Promise<any>;
getAvailableFormatters(cache: boolean): Promise<string>;
}Extensible code formatter system supporting multiple programming languages and formatter tools with configurable options.
class BaseFormatter(abc.ABC):
@property
@abc.abstractmethod
def label(self) -> str: ...
@property
@abc.abstractmethod
def importable(self) -> bool: ...
@abc.abstractmethod
def format_code(self, code: str, notebook: bool, **options) -> str: ...Server-side HTTP request handlers for formatter discovery and code formatting operations.
class FormattersAPIHandler(APIHandler):
def get(self) -> None: ...
class FormatAPIHandler(APIHandler):
def post(self) -> None: ...
def setup_handlers(web_app): ...JupyterLab settings integration with support for formatter-specific options, format-on-save settings, and error handling preferences.
interface Context {
saving: boolean;
}class JupyterlabCodeFormatter {
protected client: JupyterlabCodeFormatterClient;
working: boolean;
constructor(client: JupyterlabCodeFormatterClient);
protected formatCode(code: string[], formatter: string, options: any, notebook: boolean, cache: boolean): Promise<any>;
}
### Formatter Registry
```python { .api }
SERVER_FORMATTERS: Dict[str, BaseFormatter]namespace Constants {
const PLUGIN_NAME: string;
const FORMAT_COMMAND: string;
const FORMAT_ALL_COMMAND: string;
const ICON_FORMAT_ALL_SVG: string;
const ICON_FORMAT_ALL: string;
const SETTINGS_SECTION: string;
const COMMAND_SECTION_NAME: string;
const PLUGIN_VERSION: string;
}