HTML Export#

Once you have the view in the state you want (loaded structure, chosen representations, camera orientation, etc.), you can export it to a standalone HTML file using the write_html method. This lets you embed an interactive version of the view in your own web pages.

The exported file contains:

  • the MolSysViewer widget code (via anywidget),

  • the widget state and layout,

  • the history of viewer actions (loads, visibility changes, toolbar actions) up to the moment you call write_html.

Opening this file in a browser recreates the same interactive view, without requiring a running Python kernel.

To show it, let’s first of all create a simple view.

import molsysviewer as viewer
from molsysviewer import new_view

view = new_view('1TCD')
view.show()

Exporting the current view#

The method write_html writes an HTML file containing the interactive view:

view.write_html(
    output_filename="embedding_test.html",
    title="Embedding Test",
)
  • output_filename is the path of the HTML file to create.

  • title is optional and sets the <title> of the HTML page.

By default, the exported view includes the on-canvas control buttons (Reset, Fullscreen, background toggle, Spin, Swing) so that readers of your documentation can manipulate the scene.

After the file is written, you can open it directly with your browser (for example, by double-clicking on it in your file manager or serving it from a simple web server).

Exporting without displaying the widget#

You do not need to call show() in order to export the view. This is useful when you are generating views from a script or a non-interactive environment:

from molsysviewer import new_view

view = new_view("1TCD")
# Optionally perform additional operations here (load more systems, change visibility, etc.)
view.write_html("my_view.html", title="My MolSysViewer scene")

The resulting my_view.html file is again a standalone HTML page that you can open in any modern browser.

Hiding the built-in controls#

If you prefer a minimal viewer (for example, when embedding it inside a larger application that already provides its own UI), you can disable the MolSysViewer control buttons in the exported HTML:

view.write_html(
    "my_view_no_toolbar.html",
    title="My MolSysViewer scene",
    include_controls=False,
)

Setting include_controls=False hides the on-canvas toolbar (Reset, Fullscreen, background toggle, Spin, Swing) in the generated HTML, while keeping the scene itself fully interactive.

Embedding in other documentation systems#

Because the exported HTML file is self-contained, you can reuse it in other documentation or websites. Common options include:

  • linking to the HTML file from your documentation,

  • serving it from a static web server,

  • embedding it in another page with an <iframe>.

If you are using Sphinx to produce your web documentation, see also the dedicated example in sphinx_html_embedding.ipynb, which demonstrates how to integrate the exported HTML into a Sphinx-based documentation site.