Skip to main content

Create custom file viewers

Datagrok provides a way to define custom file viewers that are used by the file share browser. This could be done by defining a function annotated in a special way. It should take a single argument of type file, return a view, and have at least the fileViewer role, and declare supported file extensions via meta.fileViewer. This is it!

The following example defines a file viewer for .mol, .sdf, and .cif files by visualizing them with the NglViewer.

//meta.role: fileViewer
//meta.fileViewer: mol,sdf,cif
//input: file file
//output: view v
nglStructureViewer(file) {
let view = DG.View.create();
var host = ui.div([], 'd4-ngl-viewer');
var stage = new NGL.Stage(host);

file
.readAsBytes()
.then(bytes => stage.loadFile(new Blob([bytes])));

view.append(host);
return view;
}

This is it! Once a package containing that function is published, the platform will automatically create the corresponding viewer when a user selects a file with the specified extension. Here's how it looks:

file-shares-file-viewers

See also: