A JupyterLab extension to facilitate invocation of code formatters for multiple programming languages.
—
JupyterLab user interface integration providing toolbar buttons, menu items, command palette integration, keyboard shortcuts, and format-on-save functionality.
The primary extension class that implements JupyterLab's widget extension interface and orchestrates all UI components.
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;
}Parameters:
app - JupyterLab application instancetracker - Notebook tracker for managing notebook widgetspalette - Command palette for adding commandssettingRegistry - Settings registry for configurationmenu - Main menu for adding menu itemseditorTracker - File editor tracker for managing editor widgetsMethods:
createNew() - Creates toolbar button and connects save event handlers for notebooksThe main plugin export that configures the JupyterLab extension.
const plugin: JupyterFrontEndPlugin<void>;Plugin Properties:
id - Plugin identifierautoStart - Automatically start on JupyterLab loadrequires - Required JupyterLab servicesactivate - Activation function that creates the main extension instanceCore constants used throughout the extension.
namespace Constants {
const PLUGIN_NAME: string; // "jupyterlab_code_formatter"
const FORMAT_COMMAND: string; // Format selected cells command
const FORMAT_ALL_COMMAND: string; // Format all cells command
const ICON_FORMAT_ALL_SVG: string; // SVG icon for format button
const ICON_FORMAT_ALL: string; // CSS class for format icon
const SETTINGS_SECTION: string; // Settings registry section
const COMMAND_SECTION_NAME: string; // Command palette category
const PLUGIN_VERSION: string; // Plugin version
}import { JupyterFrontEnd, JupyterFrontEndPlugin } from '@jupyterlab/application';
import { ICommandPalette } from '@jupyterlab/apputils';
import { INotebookTracker } from '@jupyterlab/notebook';
import { ISettingRegistry } from '@jupyterlab/settingregistry';
import { IMainMenu } from '@jupyterlab/mainmenu';
import { IEditorTracker } from '@jupyterlab/fileeditor';
const plugin: JupyterFrontEndPlugin<void> = {
id: 'jupyterlab_code_formatter',
autoStart: true,
requires: [
ICommandPalette,
INotebookTracker,
ISettingRegistry,
IMainMenu,
IEditorTracker
],
activate: (
app: JupyterFrontEnd,
palette: ICommandPalette,
tracker: INotebookTracker,
settingRegistry: ISettingRegistry,
menu: IMainMenu,
editorTracker: IEditorTracker
) => {
new JupyterLabCodeFormatter(
app,
tracker,
palette,
settingRegistry,
menu,
editorTracker
);
}
};
export default plugin;import { Constants } from './constants';
// Register commands
app.commands.addCommand(Constants.FORMAT_COMMAND, {
execute: async () => {
// Format selected cells
},
label: 'Format cell'
});
app.commands.addCommand(Constants.FORMAT_ALL_COMMAND, {
execute: async () => {
// Format all cells
},
label: 'Format notebook'
});The extension adds a format button to notebook toolbars:
Formatter-specific menu items are added to the Edit menu:
Commands are registered with the command palette:
Right-click context menu integration:
.jp-Notebook (notebook cells)JupyterLab settings system integration:
jupyterlab_code_formatter:settingsFormat-on-save functionality:
formatOnSave setting is enabledInstall with Tessl CLI
npx tessl i tessl/pypi-jupyterlab-code-formatter