gtrace.draw.viewer

gtrace.draw.viewer

Self-contained HTML output for gtrace scenes (Stage 1 of the GUI).

renderHTML() writes a single HTML file containing the scene as embedded JSON plus an inline copy of the dependency-free JavaScript viewer (viewer.js / viewer.css, which live next to this module). The file has no external references at all, so it can be opened by double-clicking, sent to a collaborator or archived, and it still provides zoom, pan and click readout of the beam parameters.

Typical use, replacing the DXF workflow:

from gtrace.draw.viewer import renderHTML renderHTML(canvas, beams, ‘trace.html’)

or, from an OpticalLayout:

layout.render_html(‘trace.html’) layout.show()

Submodules

Attributes

__author__

__copyright__

__credits__

__license__

__version__

__maintainer__

__email__

__status__

_ASSET_DIR

Functions

_read_asset(name)

viewer_js()

Return the source of the JavaScript viewer core as a string.

viewer_css()

Return the style sheet of the viewer as a string.

viewer_template()

Return the HTML template used by renderHTML() as a string.

_escape_html(s)

Escape a string for inclusion in HTML text content.

_js_literal(obj)

Encode an object as a JavaScript literal that is safe to embed in a

renderHTML(canvas[, beams, filename, title, optics, scene])

Render a canvas and the corresponding beams into a self-contained

html_render_func([beams, optics])

Return a render_func(canvas, filename) suitable for

Package Contents

gtrace.draw.viewer.__author__ = 'Yoichi Aso'
gtrace.draw.viewer.__credits__ = ['Yoichi Aso']
gtrace.draw.viewer.__license__ = 'BSD'
gtrace.draw.viewer.__version__ = '0.8.0'
gtrace.draw.viewer.__maintainer__ = 'Yoichi Aso'
gtrace.draw.viewer.__email__ = 'asoy01@gmail.com'
gtrace.draw.viewer.__status__ = 'Beta'
gtrace.draw.viewer._ASSET_DIR
gtrace.draw.viewer._read_asset(name)
gtrace.draw.viewer.viewer_js()

Return the source of the JavaScript viewer core as a string.

gtrace.draw.viewer.viewer_css()

Return the style sheet of the viewer as a string.

gtrace.draw.viewer.viewer_template()

Return the HTML template used by renderHTML() as a string.

gtrace.draw.viewer._escape_html(s)

Escape a string for inclusion in HTML text content.

gtrace.draw.viewer._js_literal(obj)

Encode an object as a JavaScript literal that is safe to embed in a <script> element.

json.dumps with ensure_ascii=True already escapes every non-ASCII character. The only remaining hazard is the sequence ‘</’ (as in ‘</script>’) appearing inside a string value, which would terminate the script element; inside a JavaScript string literal ‘</’ is an equivalent, harmless spelling.

gtrace.draw.viewer.renderHTML(canvas, beams=None, filename='gtrace.html', title=None, optics=None, scene=None)

Render a canvas and the corresponding beams into a self-contained HTML file.

Unlike renderDXF, the output carries the physical parameters of the beams as well as the drawing, so the viewer can report the beam parameters (q, radius, ROC, waist, Gouy phase) at an arbitrary point clicked along a beam.

Parameters:
  • canvas (draw.Canvas) – The canvas to render. Ignored if scene is given.

  • beams (list of GaussianBeam or None, optional) – The beams whose parameters are made available to the viewer. If None, the viewer only shows the drawing (no click readout).

  • filename (str, optional) – Name of the HTML file to write. Defaults to ‘gtrace.html’.

  • title (str or None, optional) – Title of the page, shown in the browser tab. Defaults to the base name of filename.

  • optics (list of Optics or None, optional) – The optics of the system. Without them the viewer draws the elements but cannot say which is which, so clicking one shows nothing. Pass them to get the properties panel.

  • scene (dict or None, optional) – A scene dict as returned by serialize.scene_to_dict(). If given, it is used directly and canvas / beams / optics are ignored. This is the entry point for callers that already hold a serialized scene.

Returns:

filename – The name of the file that was written.

Return type:

str

gtrace.draw.viewer.html_render_func(beams=None, optics=None, **kwargs)

Return a render_func(canvas, filename) suitable for gtrace.draw.tools.drawOptSys, producing HTML instead of DXF.

drawOptSys(optList, beamList, ‘trace.html’,

render_func=html_render_func(beamList, optList))

The beams and the optics have to be passed explicitly because drawOptSys only hands the canvas to its renderer, while the viewer also needs the physical parameters of the beams and the identity of the elements.