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

Render static images for ASE #736

Closed
jan-janssen opened this issue Apr 5, 2018 · 9 comments
Closed

Render static images for ASE #736

jan-janssen opened this issue Apr 5, 2018 · 9 comments

Comments

@jan-janssen
Copy link
Contributor

I try to render static images of an ASE structure:

from ase import Atoms
import nglview

d = 2.9
L = 10.0
wire = Atoms('Au', positions=[[0, L / 2, L / 2]], cell=[d, L, L], pbc=[1, 0, 0])
view = nglview.show_ase(wire)
view.add_spacefill(radius_type='vdw', scale=0.5)
view.add_unitcell()
view.render_image()
view._display_image()

I get the following error message:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
~/anaconda/lib/python3.5/site-packages/IPython/core/formatters.py in __call__(self, obj, include, exclude)
    968 
    969             if method is not None:
--> 970                 return method(include=include, exclude=exclude)
    971             return None
    972         else:

~/anaconda/lib/python3.5/site-packages/IPython/core/display.py in _repr_mimebundle_(self, include, exclude)
   1190         if self.embed:
   1191             mimetype = self._mimetype
-> 1192             data, metadata = self._data_and_metadata(always_both=True)
   1193             if metadata:
   1194                 metadata = {mimetype: metadata}

~/anaconda/lib/python3.5/site-packages/IPython/core/display.py in _data_and_metadata(self, always_both)
   1199     def _data_and_metadata(self, always_both=False):
   1200         """shortcut for returning metadata with shape information, if defined"""
-> 1201         b64_data = b2a_base64(self.data).decode('ascii')
   1202         md = {}
   1203         if self.metadata:

TypeError: a bytes-like object is required, not 'str'
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-1-3319cfd15b05> in <module>()
      9 view.add_unitcell()
     10 view.render_image()
---> 11 view._display_image()

~/anaconda/lib/python3.5/site-packages/IPython/core/displayhook.py in __call__(self, result)
    255             self.start_displayhook()
    256             self.write_output_prompt()
--> 257             format_dict, md_dict = self.compute_format_data(result)
    258             self.update_user_ns(result)
    259             self.fill_exec_result(result)

~/anaconda/lib/python3.5/site-packages/IPython/core/displayhook.py in compute_format_data(self, result)
    149 
    150         """
--> 151         return self.shell.display_formatter.format(result)
    152 
    153     # This can be set to True by the write_output_prompt method in a subclass

~/anaconda/lib/python3.5/site-packages/IPython/core/formatters.py in format(self, obj, include, exclude)
    148             return {}, {}
    149 
--> 150         format_dict, md_dict = self.mimebundle_formatter(obj, include=include, exclude=exclude)
    151 
    152         if format_dict or md_dict:

TypeError: 'NoneType' object is not iterable

I tired Nglview versions 1.1.2 and 0.6.2.3, but was not able to get it to work.

@hainm
Copy link
Collaborator

hainm commented Apr 5, 2018

hi, it's a known bug in nglview if you're using ipython 6.

ipython/ipython#10982

The current workaround is to use ipython 5.3. We will try to fix and make a new release.

@jan-janssen
Copy link
Contributor Author

I tried to compile the example on mybinder: https://mybinder.org/v2/gh/jan-janssen/lammps-notebooks.git/ase-notebooks

The environment includes:

name: ase-notebooks
dependencies:
- python=3.6
- nomkl
- nglview=1.0
- ipywidgets==7.0.0
- widgetsnbextension==3.0.0
- ipykernel=4.6
- ipython=5.3
- ase

channels:
  - anaconda
  - bioconda
  - conda-forge

Basically inspired by yours https://github.com/hainm/notebook-nglview/blob/master/environment.yml as well as the requirements:

jupyter
git+https://github.com/arose/nglview.git

But I only get an image not found error!

@hainm
Copy link
Collaborator

hainm commented Apr 5, 2018

I will have a look tonight. cheers.

@hainm
Copy link
Collaborator

hainm commented Apr 6, 2018

Please try the master branch again. I have a fix here (turns out it's much simpler than I though). f6b665d

screen shot 2018-04-06 at 12 41 34 am

@hainm
Copy link
Collaborator

hainm commented Apr 6, 2018

uhm, one more point. In your example, you need to run the code in three Cells.

# 1st cell
from ase import Atoms
import nglview

d = 2.9
L = 10.0
wire = Atoms('Au', positions=[[0, L / 2, L / 2]], cell=[d, L, L], pbc=[1, 0, 0])
view = nglview.show_ase(wire)
view.add_spacefill(radius_type='vdw', scale=0.5)
view.add_unitcell()
view # display it

# 2nd cell
view.render_image()

# 3rd cell
view._display_image()

@hainm
Copy link
Collaborator

hainm commented Apr 6, 2018

If you just want to get the image (e.g: for presentation), you can use

# 1st cell
view # display

# 2nd cell
view.download_image() # see its options.

@hainm
Copy link
Collaborator

hainm commented Apr 6, 2018

screen shot 2018-04-06 at 12 51 53 am

@jan-janssen
Copy link
Contributor Author

Thanks a lot, locally everything is working fine now, I just realised that http://mybinder.org has issues loading nglview in general. I tried your notebook and that is not working either: https://hub.mybinder.org/user/hainm-nglview-notebooks-fhq6if62/notebooks/pytraj.ipynb so it seems to be binder related.

@hainm
Copy link
Collaborator

hainm commented Apr 6, 2018

cool.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants