Skip to main content

Streamlit Oxview Component

A Streamlit component for the display of coarse-grained DNA/RNA 3D models. It is a wrapper around the oxdna-viewer. The viewer's own colour-bar legend is not drawn, so the 3D view fills the frame.

Installation

Python 3.10 or newer. This component requires access to write files to a cache directory.

pip install st_oxview

The component serves its assets from disk, from a per-user cache directory (~/Library/Caches/st_oxview on macOS, %LOCALAPPDATA%\st_oxview\cache on Windows, $XDG_CACHE_HOME/st_oxview elsewhere) rather than the system temp directory, which the OS reaps while a long-lived app is still open.

Example

Alt Text

Look at the example for a streamlit Web App:

Streamlit App

Usage

Display with drag and drop

You can make a empty oxview fram and drag and drop the files to the frame.

import streamlit as st
from st_oxview import oxview_from_file

success = oxview_from_file()

Just drag and drop the files to the frame.

Display from file paths

import streamlit as st
from st_oxview import oxview_from_file

success = oxview_from_file(configuration=path_to_conf, # path to the configuration file
                           topology=path_to_topo,      # path to the topology file
                           forces=None,                # path to the forces file
                           pdb=None,                   # path to the pdb file
                           js_script=None,             # path to the javascript script file
                           colormap=None,              # name of the Matplotlib colormap
                           index_colors=(),            # one integer per nucleotide, spread over the colormap
                           selected=(),                # nucleotide indices to select in the viewer
                           frame_id=None,              # ID of the oxView frame: if a frame ID is set, it will be reused instead of creating a new frame
                           width='99%',                # width of the viewer frame
                           height=500,                 # height of the viewer frame
                           reset_scene=True,           # clear the scene before loading the structure
                           reset_camera=False,         # also recentre the camera when reusing a frame
                           scale_bar=False,            # draw a nanometre scale bar in the corner
                           pick=False,                 # report clicks in the 3D view
                           key=None,                   # streamlit component key
                        )

When pick is False, the component returns True if the visualization is successfully created and False otherwise. When pick is True, the component returns the clicked nucleotide's index the first run after a click, otherwise None.

Display from text

import streamlit as st
from st_oxview import oxview_from_text

with open("configuration.dat", "r") as f:
    conf_text = f.read()

with open("topology.top", "r") as f:
    topo_text = f.read()

success = oxview_from_text(configuration=conf_text, # text of the configuration file
                           topology=topo_text,      # text of the topology file
                           forces=None,             # text of the forces file
                           pdb=None,                # text of the pdb file
                           js_script=None,          # text of the javascript script file
                           colormap=None,           # name of the Matplotlib colormap
                           index_colors=(),         # one integer per nucleotide, spread over the colormap
                           selected=(),             # nucleotide indices to select in the viewer
                           width='99%',             # width of the viewer frame
                           height=500,              # height of the viewer frame
                           reset_scene=True,        # clear the scene before loading the structure
                           reset_camera=False,      # also recentre the camera when reusing a frame
                           scale_bar=False,         # draw a nanometre scale bar in the corner
                           pick=False,              # report clicks in the 3D view
                           key=None)                # streamlit component key

When pick is False, the component returns True if the visualization is successfully created and False otherwise. When pick is True, the component returns the clicked nucleotide's index the first run after a click, otherwise None.

Colouring the nucleotides

index_colors takes one integer per nucleotide, in load order (the order of the topology file). The chosen colormap is stretched over the range of those values, so range(n) paints a gradient along the structure, while 0/1 flags paint two groups.

success = oxview_from_text(configuration=conf_text,
                           topology=topo_text,
                           colormap="Reds",
                           index_colors=list(range(n_nucleotides)),
                           key="gradient")

Picking and selecting

pick=True reports clicks in the 3D view: the function returns the clicked nucleotide's index — numbered exactly like index_colors — on the first run after the click, and None afterwards, so an old click cannot keep overriding a selection made some other way. It needs a key.

selected goes the other way: the nucleotides whose indices you pass get oxView's own selection — its selection colour on top of whatever they are already coloured with, and a slightly larger scale. The colouring is left alone, so a colormap overlay stays visible underneath. Re-sending the structure clears the selection, so selected is re-applied on every render.

Together they make a two-way link between the 3D view and the rest of the app:

import streamlit as st
from st_oxview import oxview_from_text

if "picked" not in st.session_state:
    st.session_state.picked = []

clicked = oxview_from_text(configuration=conf_text,
                           topology=topo_text,
                           colormap="Reds",
                           index_colors=list(range(n_nucleotides)),
                           selected=st.session_state.picked,
                           frame_id=1,
                           pick=True,
                           key="viewer")

if clicked is not None:
    # select the whole strand/domain the clicked nucleotide belongs to
    st.session_state.picked = my_domain_of(clicked)
    st.rerun()

Reusing a frame

Pass the same frame_id to keep rendering into the same viewer instead of building a new one on every rerun — the camera then stays where the user put it. reset_scene=True (the default) clears the scene before loading, which is what you want when the structure itself changed; with reset_scene=False the files are dropped onto the structure already in the viewer, which is how a colour overlay is updated without re-sending the whole model. reset_camera=True additionally recentres the camera.

Display from an OxView JSON file

You can load a saved OxView scene directly from a .oxview file with oxview_from_json.

From a local .oxview file

import streamlit as st
from st_oxview import oxview_from_json

with open("scene.oxview", "r", encoding="utf-8") as f:
    scene_text = f.read()

success = oxview_from_json(scene_data=scene_text,  # text content of the .oxview file
                           frame_id=None,          # if None, the component creates a new frame
                           width='99%',            # width of the viewer frame
                           height=500,             # height of the viewer frame
                           key='oxview_json')      # streamlit component key

From a Streamlit file uploader

import streamlit as st
from st_oxview import oxview_from_json

json_file = st.file_uploader("Upload an OxView file", type=["oxview"])

if json_file is not None:
    success = oxview_from_json(scene_data=json_file.getvalue(),  # uploaded file content
                               frame_id=None,
                               width='99%',
                               height=500,
                               key='oxview_json_upload')

Scene_data can be provided as:

  • a Python dict
  • a JSON str
  • the bytes returned by st.file_uploader(...).getvalue()

The function returns a boolean value indicating whether the scene was successfully processed and displayed.

Housekeeping

Each process copies the component's assets into its own folder under the cache directory and removes it again when it exits. That last step only happens on a clean exit, so an app that is killed or crashes leaves its folder behind — assets and all — and the cache directory is deliberately one the OS does not reap, so nothing else would ever clear it.

Both of these run by themselves; they are exported so an app can also tidy up on demand:

from st_oxview import clear_cache, trim_temp_files

clear_cache()            # drop the folders of viewers whose process is gone
                         # (called on import, so a restart already cleans up)
trim_temp_files(keep=32) # drop the per-render files no longer needed
                         # (called after every render)

Every render writes a fresh set of files — configuration, topology, colour overlay, injected scripts — into the working folder, so without trim_temp_files a long session with a large structure grows that folder by megabytes per rerun.

Tests

The 3D view is oxView in an iframe and is not testable from here, but the bookkeeping around it is: the working folders on disk, and turning a click reported by the viewer into a value the calling app sees exactly once.

pytest

How to cite:

Please include this citation if the OxView Component is used in an academic study:

Lucandia. Lucandia/st_oxview; Zenodo, 2024. https://zenodo.org/doi/10.5281/zenodo.12515559.

DOI

License

Code is licensed under the GNU General Public License v3.0 (GPL-3.0)

License: GPL-3.0

Metadata

Release files for st-oxview 2.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for st-oxview 2.0.0
File Size Uploaded Provenance Actions
st_oxview-2.0.0.tar.gz 2.3 MB Without provenance Details for st_oxview-2.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for st-oxview 2.0.0
File Interpreter ABI Platform Actions
st_oxview-2.0.0-py3-none-any.whl Python 3 none any Details for st_oxview-2.0.0-py3-none-any.whl

Total release size: 4.7 MB

Release files / st_oxview-2.0.0.tar.gz

Download URL st_oxview-2.0.0.tar.gz
Size 2.3 MB
Tags Source
SHA-256 checksum
How to use checksums
ef5a02dcf59c4611e5496efe79b4e1522e1983089d5fafe56d01027e80b733f4
BLAKE2b-256 checksum
How to use checksums
a324938a758e45a6ee3f6a2407b7ebd07a8ab1dc38dccd9d17ddf2fba265c7c9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.2

Release files / st_oxview-2.0.0-py3-none-any.whl

Download URL st_oxview-2.0.0-py3-none-any.whl
Size 2.4 MB
Tags Python 3
SHA-256 checksum
How to use checksums
abc035653d5a652ee74a3bdbc80dbe200569ea3d8b476a914df73bc10d6806df
BLAKE2b-256 checksum
How to use checksums
75ffd81527ac7c81b27eca94ce54e474f1833e834a8ec2f7bdc2b33617b9ab3f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.2

Security for st-oxview 2.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page