higlass / higlass-python

Python bindings to and Jupyter Notebook+Lab integration for the HiGlass viewer
http://docs-python.higlass.io/
MIT License
52 stars 12 forks source link

feat: temporarily enable a renderer #113

Closed manzt closed 1 year ago

manzt commented 1 year ago

Description

What was changed in this pull request?

Adds a context manager to higlass._display.RendererRegistry, which allows for temporarily enabling a renderer with a context manager.

with hg.renderers.enable("custom") as custom_render:
    custom_render(...)

assert hg.renderers.active == "default"

I think this will be useful for creating a separate (Jupyter-less) renderer for higlass-python, allowing for opening through a script (addressing #95):

For example, a simple script example:

import higlass as hg

def open_browser(conf)
    with hg.renderers.enable("html") as render:
        html = render(conf.dict())
    encoded = urllib.parse.quote(html)
    # there is a limit on data URL sizes, so might need to integrate
    # with hg server here to dynamically create and HTML endpoint
    webbrowser.open("data:text/html," + encoded)

if __name__ == "__main__":
    ts = hg.cooler("../data/dataset.mcool")
    track = ts.track("heatmap")
    view = hg.view(track)
    open_browser(view.viewconf())
    input("Press any key to exit.")

Why is it necessary?

Fixes #___

Checklist

manzt commented 1 year ago

please ignore the linting stuff. There seems to be some issue with how higlass is detected in CI vs on my local machine for ruff's (isort) import sorting for things outside of src/.

Locally, for test_test*.py ruff wants :

import pytest # third party

import higlass as hg # local

whereas in CI it wants:

import higlass as hg 
import pytest

which makes me think for some reason higlass is being detected as a third-party module outside of src in CI. The current config just turns off this rule when looking outside of src (but in CI only). This way we still enforce import sorting locally (and it will be automatically fixed with ruff --fix .), but we just don't make it a criteria for passing CI.