nglview.widget

Module contents

class nglview.widget.NGLWidget(structure=None, representations=None, parameters=None, **kwargs)[source]

Bases: ipywidgets.widgets.domwidget.DOMWidget

add_component(filename, **kwargs)[source]

add component from file/trajectory/struture

filename : str or Trajectory or Structure or their derived class or url **kwargs : additional arguments, optional

>>> view = nglview.Widget()
>>> view
>>> view.add_component(filename)

If you want to load binary file such as density data, mmtf format, it is faster to load file from current or subfolder.

add_pdbid(pdbid)[source]

add new Structure view by fetching pdb id from rcsb

>>> view = nglview.NGLWidget()
>>> view.add_pdbid('1tsu')
>>> # which is equal to 
>>> # view.add_component('rcsb://1tsu.pdb')
add_representation(repr_type, selection='all', **kwargs)[source]

Add structure representation (cartoon, licorice, ...) for given atom selection.

repr_type
: str
type of representation. Please see http://arose.github.io/ngl/api/ for further info.
selection
: str or 1D array (atom indices) or any iterator that returns integer, default ‘all’
atom selection

**kwargs: additional arguments for representation

>>> import nglview as nv
>>> import pytraj as pt
>>> t = (pt.datafiles.load_dpdp()[:].superpose('@CA'))
>>> w = nv.show_pytraj(t)
>>> w.add_representation('cartoon', selection='protein', color='blue')
>>> w.add_representation('licorice', selection=[3, 8, 9, 11], color='red')
>>> w

User can also use shortcut

>>> w.add_cartoon(selection) # w.add_representation('cartoon', selection)
add_structure(structure, **kwargs)[source]

add structure to view

structure : nglview.Structure object

>>> view.add_trajectory(traj0)
>>> view.add_trajectory(traj1)
>>> # then add Structure
>>> view.add_structure(...)

nglview.NGLWidget.add_component

add_trajectory(trajectory, **kwargs)[source]

add new trajectory to view

trajectory: nglview.Trajectory or its derived class or
pytraj.Trajectory-like, mdtraj.Trajectory or MDAnalysis objects

nglview.NGLWidget.add_component

>>> import nglview as nv, pytraj as pt
>>> traj = pt.load(nv.datafiles.TRR, nv.datafiles.PDB)
>>> view = nv.show_pytraj(view)
>>> # show view first
>>> view
>>> # add new Trajectory
>>> traj2 = pt.datafiles.load_tz2()
>>> view.add_trajectory(traj2)
background

A trait for unicode strings.

camera
center(*args, **kwargs)[source]

alias of center_view

center_view(zoom=True, selection='*', component=0)[source]

center view for given atom selection

view.center_view(selection=‘1-4’)

clear(*args, **kwargs)[source]

shortcut of clear_representations

clear_representations(component=0)[source]

clear all representations for given component

component
: int, default 0 (first model)
You need to keep track how many components you added.
color_by(color_scheme, component=0)[source]

update color for all representations of given component

Unstable feature

color_scheme : str component : int, default 0

component index
>>> # component 0
>>> view.color_by('atomindex')
>>> # component 1
>>> view.color_by('atomindex', component=1)
coordinates_dict
out
: dict of numpy 3D-array, dtype=’f4’
coordinates of trajectories at current frame
count

An int trait.

detach(split=False)[source]

detach player from its original container.

split
: bool, default False
if True, resize notebook then move it to the right of its container
display(gui=False, use_box=False)[source]
download_image(filename='screenshot.png', factor=4, antialias=True, trim=False, transparent=False)[source]

render and download scence at current frame

filename : str, default ‘screenshot.png’ factor : int, default 4

quality of the image, higher is better

antialias : bool, default True trim : bool, default False transparent : bool, default False

frame

An int trait.

hide(indices)[source]

set invisibility for given component/struture/trajectory (by their indices)

loaded

A boolean (True, False) trait.

n_components

An int trait.

on_frame_changed
on_loaded
on_update_dragged_file
orientation

An instance of a Python list.

parameters
picked

An instance of a Python dict.

remove_component(component_id)[source]

remove component by its uuid

>>> view.add_trajectory(traj0)
>>> view.add_trajectory(traj1)
>>> view.add_struture(structure)
>>> # remove last component
>>> view.remove_component(view._ngl_component_ids[-1])
render_image(frame=None, factor=4, antialias=True, trim=False, transparent=False)[source]

render and get image as ipywidgets.widget_image.Image

frame
: int or None, default None
if None, use current frame if specified, use this number.
factor
: int, default 4
quality of the image, higher is better

antialias : bool, default True trim : bool, default False transparent : bool, default False

# tell NGL to render send image data to notebook. view.render_image()

# make sure to call get_image method view.get_image()

You need to call render_image and get_image in different notebook’s Cells

representations
selector

A trait for unicode strings.

set_representations(representations, component=0)[source]

representations : list of dict

show(**kwargs)[source]

shortcut of show_only

show_only(indices='all')[source]

set visibility for given components (by their indices)

indices : {‘all’, array-like}, component index, default ‘all’

sync_view()[source]

call this if you want to sync multiple views of a single viewer

Note: unstable feature

update_representation(component=0, repr_index=0, **parameters)[source]
component
: int, default 0
component index
repr_index
: int, default 0
representation index for given component

parameters : dict

class nglview.widget.ComponentViewer(view, index)[source]

Bases: object

Convenient attribute for NGLWidget. See example below.

>>> view = nv.NGLWidget()
>>> view.add_trajectory(traj) # traj is a component 0
>>> view.add_component(filename) # component 1
>>> view.component_0.clear_representations()
>>> view.component_0.add_cartoon()
>>> view.component_1.add_licorice()
>>> view.remove_component(view.comp1.id)
add_representation(repr_type, selection='all', **kwargs)[source]
hide()[source]

set invisibility for given components (by their indices)

id
show()[source]

set invisibility for given components (by their indices)