Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
""" HTML Generator for JSmol
This is all an evil iframe hack to get JSmol to display 3-d graphics while separating JSmol's j2s machinery from your actual web page.
There are some caveats for how to load JSmol, in particular it cannot just load its code from a ``file://`` uri. To use a html file generated by this module, you need
* A web server,
* The JSmol directory tree must be served by your web server,
* The output of :meth:`JSMolHtml.inner_html` or :meth:`JSMolHtml.outer_html` must be served by the same web server.
See https://github.com/phetsims/molecule-polarity/issues/6 for a discussion of loading JSMol. """
""" <html> <head> <style> * {{ margin: 0; padding: 0; overflow: hidden; }} body, html {{ height: 100%; width: 100%; }} </style> <script type="text/javascript" src="{path_to_jsmol}/JSmol.min.js"></script> </head> <body> <script type="text/javascript"> var script = {script}; var Info = {{ width: '{width}', height: '{height}', debug: false, disableInitialConsole: true, // very slow when used with inline mesh color: '#3131ff', addSelectionOptions: false, use: 'HTML5', j2sPath: '{path_to_jsmol}/j2s', script: script, }}; var jmolApplet0 = Jmol.getApplet('jmolApplet0', Info); </script> </body> </html> """
""" <iframe srcdoc="{escaped_inner_html}" width="{width}" height="{height}" style="border: 0;"> </iframe> """
""" <html> <head> <title>JSmol 3D Scene</title> </head> </body> {iframe} </body> </html> """.format(iframe=IFRAME_TEMPLATE)
""" INPUT:
- ``jmol`` -- 3-d graphics or :class:`sage.repl.rich_output.output_graphics3d.OutputSceneJmol` instance. The 3-d scene to show.
- ``path_to_jsmol`` -- string (optional, default is ``'/nbextensions/jsmol'``). The path (relative or absolute) where ``JSmol.min.js`` is served on the web server.
- ``width`` -- integer or string (optional, default: ``'100%'``). The width of the JSmol applet using CSS dimensions.
- ``height`` -- integer or string (optional, default: ``'100%'``). The height of the JSmol applet using CSS dimensions.
EXAMPLES::
sage: from sage.repl.display.jsmol_iframe import JSMolHtml sage: JSMolHtml(sphere(), width=500, height=300) JSmol Window 500x300 """ else: self._path = path_to_jsmol
def script(self): r""" Return the JMol script file.
This method extracts the Jmol script from the Jmol spt file (a zip archive) and inlines meshes.
OUTPUT:
String.
EXAMPLES::
sage: from sage.repl.display.jsmol_iframe import JSMolHtml sage: from sage.repl.rich_output.output_graphics3d import OutputSceneJmol sage: jsmol = JSMolHtml(OutputSceneJmol.example(), width=500, height=300) sage: jsmol.script() 'data "model list"\n10\nempt...aliasdisplay on;\n' """ command, obj, meshfile = line.split(' ', 3) assert command == 'pmesh' if meshfile not in ['dots\n', 'mesh\n']: assert meshfile.startswith('"') and meshfile.endswith('"\n') meshfile = meshfile[1:-2] # strip quotes script += [ 'pmesh {0} inline "'.format(obj), self._zip.open(meshfile).read(), '"\n' ] else: script += [line] else:
r""" The :meth:`script` as Javascript string.
Since the many shortcomings of Javascript include multi-line strings, this actually returns Javascript code to reassemble the script from a list of strings.
OUTPUT:
String. Javascript code that evaluates to :meth:`script` as Javascript string.
EXAMPLES::
sage: from sage.repl.display.jsmol_iframe import JSMolHtml sage: from sage.repl.rich_output.output_graphics3d import OutputSceneJmol sage: jsmol = JSMolHtml(OutputSceneJmol.example(), width=500, height=300) sage: print(jsmol.js_script()) [ 'data "model list"', ... 'isosurface fullylit; pmesh o* fullylit; set antialiasdisplay on;', ].join('\n'); """
""" Return as string representation
OUTPUT:
String.
EXAMPLES::
sage: from sage.repl.display.jsmol_iframe import JSMolHtml sage: from sage.repl.rich_output.output_graphics3d import OutputSceneJmol sage: JSMolHtml(OutputSceneJmol.example(), width=500, height=300)._repr_() 'JSmol Window 500x300' """
""" Return a HTML document containing a JSmol applet
EXAMPLES::
sage: from sage.repl.display.jsmol_iframe import JSMolHtml sage: from sage.repl.rich_output.output_graphics3d import OutputSceneJmol sage: jmol = JSMolHtml(OutputSceneJmol.example(), width=500, height=300) sage: print(jmol.inner_html()) <html> <head> <style> * { margin: 0; padding: 0; ... </html> """ script=self.js_script(), width=self._width, height=self._height, path_to_jsmol=self._path, )
""" Return HTML iframe
OUTPUT:
String.
EXAMPLES::
sage: from sage.repl.display.jsmol_iframe import JSMolHtml sage: from sage.repl.rich_output.output_graphics3d import OutputSceneJmol sage: jmol = JSMolHtml(OutputSceneJmol.example()) sage: print(jmol.iframe()) <iframe srcdoc=" ... </iframe> """ script=self.js_script(), width=self._width, height=self._height, escaped_inner_html=escaped_inner_html, )
""" Return a HTML document containing an iframe with a JSmol applet
OUTPUT:
String
EXAMPLES::
sage: from sage.repl.display.jsmol_iframe import JSMolHtml sage: from sage.repl.rich_output.output_graphics3d import OutputSceneJmol sage: jmol = JSMolHtml(OutputSceneJmol.example(), width=500, height=300) sage: print(jmol.outer_html()) <html> <head> <title>JSmol 3D Scene</title> </head> </body> <BLANKLINE> <iframe srcdoc=" ... </html> """ script=self.js_script(), width=self._width, height=self._height, escaped_inner_html=escaped_inner_html, )
|