Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plugin for TSV files view. Closes #4226 and #2746 #4684

Merged
merged 4 commits into from Jun 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
81 changes: 66 additions & 15 deletions packages/csvviewer-extension/src/index.ts
Expand Up @@ -10,52 +10,59 @@ import {
} from '@jupyterlab/apputils';

import {
CSVViewer, CSVViewerFactory
CSVViewer, CSVViewerFactory, TSVViewerFactory
} from '@jupyterlab/csvviewer';

import {
IDocumentWidget
} from '@jupyterlab/docregistry';

/**
* The name of the factory that creates CSV widgets.
* The name of the factories that creates widgets.
*/
const FACTORY = 'Table';
const FACTORY_CSV = 'CSVTable';
const FACTORY_TSV = 'TSVTable';


/**
* The table file handler extension.
* The CSV file handler extension.
*/
const plugin: JupyterLabPlugin<void> = {
activate,
id: '@jupyterlab/csvviewer-extension:plugin',

const csv: JupyterLabPlugin<void> = {
activate: activateCsv,
id: '@jupyterlab/csvviewer-extension:csv',
requires: [ILayoutRestorer],
autoStart: true
};


/**
* Export the plugin as default.
* The TSV file handler extension.
*/
export default plugin;
const tsv: JupyterLabPlugin<void> = {
activate: activateTsv,
id: '@jupyterlab/csvviewer-extension:tsv',
requires: [ILayoutRestorer],
autoStart: true
};


/**
* Activate the table widget extension.
* Activate cssviewer extension for CSV files
*/
function activate(app: JupyterLab, restorer: ILayoutRestorer): void {
function activateCsv(app: JupyterLab, restorer: ILayoutRestorer): void {
const factory = new CSVViewerFactory({
name: FACTORY,
name: FACTORY_CSV,
fileTypes: ['csv'],
defaultFor: ['csv'],
readOnly: true
});
const tracker = new InstanceTracker<IDocumentWidget<CSVViewer>>({ namespace: 'csvviewer' });
const tracker = new InstanceTracker<IDocumentWidget<CSVViewer>>({namespace: 'csvviewer'});

// Handle state restoration.
restorer.restore(tracker, {
command: 'docmanager:open',
args: widget => ({ path: widget.context.path, factory: FACTORY }),
args: widget => ({path: widget.context.path, factory: FACTORY_CSV}),
name: widget => widget.context.path
});

Expand All @@ -65,11 +72,55 @@ function activate(app: JupyterLab, restorer: ILayoutRestorer): void {
// Track the widget.
tracker.add(widget);
// Notify the instance tracker if restore data needs to update.
widget.context.pathChanged.connect(() => { tracker.save(widget); });
widget.context.pathChanged.connect(() => {
tracker.save(widget);
});

if (ft) {
widget.title.iconClass = ft.iconClass;
widget.title.iconLabel = ft.iconLabel;
}
});
}

/**
* Activate cssviewer extension for TSV files
*/
function activateTsv(app: JupyterLab, restorer: ILayoutRestorer): void {
const factory = new TSVViewerFactory({
name: FACTORY_TSV,
fileTypes: ['tsv'],
defaultFor: ['tsv'],
readOnly: true
});
const tracker = new InstanceTracker<IDocumentWidget<CSVViewer>>({namespace: 'tsvviewer'});

// Handle state restoration.
restorer.restore(tracker, {
command: 'docmanager:open',
args: widget => ({path: widget.context.path, factory: FACTORY_TSV}),
name: widget => widget.context.path
});

app.docRegistry.addWidgetFactory(factory);
let ft = app.docRegistry.getFileType('tsv');
factory.widgetCreated.connect((sender, widget) => {
// Track the widget.
tracker.add(widget);
// Notify the instance tracker if restore data needs to update.
widget.context.pathChanged.connect(() => {
tracker.save(widget);
});

if (ft) {
widget.title.iconClass = ft.iconClass;
widget.title.iconLabel = ft.iconLabel;
}
});
}

/**
* Export the plugins as default.
*/
const plugins: JupyterLabPlugin<any>[] = [csv, tsv];
export default plugins;
16 changes: 16 additions & 0 deletions packages/csvviewer/src/widget.ts
Expand Up @@ -166,6 +166,7 @@ namespace CSVViewer {
}
}


/**
* A document widget for CSV content widgets.
*/
Expand Down Expand Up @@ -218,3 +219,18 @@ class CSVViewerFactory extends ABCWidgetFactory<IDocumentWidget<CSVViewer>> {
return new CSVDocumentWidget({ context });
}
}

/**
* A widget factory for TSV widgets.
*/
export
class TSVViewerFactory extends ABCWidgetFactory<IDocumentWidget<CSVViewer>> {
/**
* Create a new widget given a context.
*/
protected createNewWidget(context: DocumentRegistry.Context): IDocumentWidget<CSVViewer> {
const delimiter = '\t';
return new CSVDocumentWidget({context, delimiter});
}
}

7 changes: 7 additions & 0 deletions packages/docregistry/src/registry.ts
Expand Up @@ -1126,6 +1126,13 @@ namespace DocumentRegistry {
mimeTypes: ['text/csv'],
iconClass: 'jp-MaterialIcon jp-SpreadsheetIcon'
},
{
name: 'tsv',
displayName: 'TSV File',
extensions: ['.tsv'],
mimeTypes: ['text/csv'],
iconClass: 'jp-MaterialIcon jp-SpreadsheetIcon'
},
{
name: 'r',
displayName: 'R File',
Expand Down