Hi Greg,

Draw.ShowMol(m) works perfectly and even works in a loop to view multiple 
molecules (one at a time). thanks!

Hi Ivan, thanks for the terminal graphics code example. I think the ASCII art 
structures are fun and useful. I've used that in Open Babel before to quickly 
view molecules. Such an ASCII structure view method in RDKit would be great and 
I would be interested in trying it.

Vin

________________________________
From: Ivan Tubert-Brohman <[email protected]>
Sent: Tuesday, September 1, 2020 9:29 AM
To: Scalfani, Vincent <[email protected]>
Cc: [email protected] <[email protected]>
Subject: [EXTERNAL] Re: [Rdkit-discuss] Display Molecules within IPython Console

Hi Vin,

If you are running the IPython console on a terminal emulator that supports 
graphics, you could display the molecule by printing out the necessary terminal 
escape codes followed by the image buffer. The solution is terminal-specific; 
here's an example that works using the Kitty terminal:

from base64 import standard_b64encode
import io

def serialize_gr_command(cmd, payload=None):
   cmd = ','.join('{}={}'.format(k, v) for k, v in cmd.items())
   ans = []
   w = ans.append
   w(b'\033_G'), w(cmd.encode('ascii'))
   if payload:
      w(b';')
      w(payload)
   w(b'\033\\')
   return b''.join(ans)

def write_chunked(cmd, data):
   data = standard_b64encode(data)
   while data:
      chunk, data = data[:4096], data[4096:]
      m = 1 if data else 0
      cmd['m'] = m
      sys.stdout.buffer.write(serialize_gr_command(cmd, chunk))
      sys.stdout.flush()
      cmd.clear()

def cat_mol(mol):
    img = Chem.Draw.MolToImage(mol)
    buf = io.BytesIO()
    img.save(buf, format='png')
    write_chunked({'a': 'T', 'f': 100}, buf.getvalue())

And then you can do something like this:
[image.png]

I'm sure the above could be adapted to work with iterm2 or other terminals that 
support graphics, but that's left as an exercise to the reader.

An alternative I often use when graphics are not available is to render the 
structure as ASCII art, but that may be an acquired taste. The above would look 
like this:

[image.png]

I'll leave the code that does that for some other day.

Best,
Ivan

On Tue, Sep 1, 2020 at 9:57 AM Scalfani, Vincent 
<[email protected]<mailto:[email protected]>> wrote:
Hello,

Is it possible to display a molecule image directly in an IPython console (not 
a Jupyter Notebook)? Or maybe I need to send the image file directly to my 
image viewer? I would like to be able to quickly view the molecules without 
using a Jupyter Notebook or having to save the PNGs. For example:

In [6]: from rdkit import Chem
   ...: from rdkit.Chem.Draw import IPythonConsole
   ...: from rdkit.Chem import Draw

In [7]: m = Chem.MolFromSmiles('c1ncncc1C(=O)[O-]')

In [8]: m
Out[8]: <rdkit.Chem.rdchem.Mol at 0x....>

In [9]: Chem.Draw.MolToImage(m)
Out[9]: <PIL.PngImagePlugin.PngImageFile image mode=RGB size=300x300 at 0x...>


Thanks for your help.

Vin


---

Vincent F. Scalfani

The University of Alabama

<mailto:[email protected]>


_______________________________________________
Rdkit-discuss mailing list
[email protected]<mailto:[email protected]>
https://lists.sourceforge.net/lists/listinfo/rdkit-discuss
_______________________________________________
Rdkit-discuss mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/rdkit-discuss

Reply via email to