nglview.widget

Module contents

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

Bases: object

Convenient attribute for NGLWidget. See example below.

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

set invisibility for given components (by their indices)

property id
set_coordinates(coordinates)[source]

coordinates : numpy.ndarray, shape=(3, n_atoms)

set_position(pos)[source]

pos: List-like of float, length = 3

set_rotation(rot)[source]

rot: List-like of float, length = 3

set_scale(scale)[source]

scale: float

show()[source]

set invisibility for given components (by their indices)

class nglview.widget.NGLWidget(*args, **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

>>> import nglview
>>> view = nglview.NGLWidget()
>>> view 
... filename = 'somefile.ccp4'
... 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, **kwargs)[source]

add new Structure view by fetching pdb id from rcsb

>>> import nglview
>>> view = nglview.NGLWidget()
>>> c = 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_typestr

type of representation. Please see http://nglviewer.org/ngl/ for further info.

selectionstr 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 
>>> t = pytraj.datafiles.load_tz2()
>>> 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

>>> selection = 'protein'
>>> 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(s)

nglview.NGLWidget.add_component

add_trajectory(trajectory, **kwargs)[source]

add new trajectory to view

trajectory: nglview.Trajectory or its derived class or

a supported object, eg pytraj.Trajectory-like, mdtraj.Trajectory, MDAnalysis objects, etc

nglview.NGLWidget.add_component

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

A trait for unicode strings.

property camera
center(selection='*', duration=0, component=0, **kwargs)[source]

center view for given atom selection

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

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

shortcut of clear_representations

clear_representations(component=0)[source]

clear all representations for given component

componentint, 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

>>> import nglview
>>> view = nglview.demo()
>>> # component 0
>>> view.color_by('atomindex')
>>> # component 1
>>> view.color_by('atomindex', component=1)
display(gui=False, style='ngl')[source]
guibool

If True: turn on GUI

stylestr, {‘ngl’, ‘ipywidgets}, default ‘ngl’

GUI style (with gui=True)

download_image(filename='screenshot.png', factor=4, antialias=True, trim=False, transparent=False)[source]

render and download scene 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.

gui_style

An enum of strings where the case should be ignored.

handle_resize()[source]
hide(indices)[source]

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

loaded

A boolean (True, False) trait.

max_frame

An int trait.

n_components

An int trait.

on_loaded
property parameters
picked

An instance of a Python dict.

remove_component(c)[source]

remove component by its uuid. If isinstance(c, ComponentViewer), c won’t be associated with self

c : Union[int, ComponentViewer]

>>> c0 = view.add_trajectory(traj0) 
... c1 = view.add_trajectory(traj1)
... c2 = view.add_struture(structure)
... # remove last component
... view.remove_component(c2)
... assert c2._view is None
render_image(frame=None, factor=4, antialias=True, trim=False, transparent=False)[source]

render and get image as ipywidgets.widget_image.Image

frameint or None, default None

if None, use current frame if specified, use this number.

factorint, 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

property representations
set_coordinates(arr_dict, movie_making=False, render_params=None)[source]

Used for update coordinates of a given trajectory >>> # arr: numpy array, ndim=2 >>> # update coordinates of 1st trajectory >>> view.set_coordinates({0: arr})# doctest: +SKIP

set_representations(representations, component=0)[source]

representations : list of dict

show(**kwargs)[source]

shortcut of show_only

show_only(indices='all', **kwargs)[source]

set visibility for given components (by their indices)

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

update_representation(component=0, repr_index=0, **parameters)[source]
componentint, default 0

component index

repr_indexint, default 0

representation index for given component

parameters : dict