Skip to content

Commit

Permalink
Update Jupyter API
Browse files Browse the repository at this point in the history
  • Loading branch information
anayden committed Dec 24, 2023
1 parent 66f585e commit ff121eb
Show file tree
Hide file tree
Showing 7 changed files with 337 additions and 446 deletions.
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Example usage in the code:

```python
from time import sleep
from r2dt_client import setup, submit, update_status_for, fetch_results_for, clear_job_cache
from r2dt_client import setup, submit, update_status_for, fetch_results_for

setup(email="YOUR_EMAIL")

Expand All @@ -58,6 +58,18 @@ fetch_results_for(job)
print(job.results['fasta'])
```

Using the widget via Jupyter Notebook:

```jupyter
!pip install r2dt_client[widget]
from r2dt_client import setup, draw
setup(email="YOUR_EMAIL")
draw(
">S box leader))\nCTCTTATCGAGAGTTGGGCGAGGGATTTGGCCTTTTGACCCCAAAAGCAACCGACCGTAATTCCATTGTGAAATGGGGCGCATTTTTTTCGCGCCGAGACGCTGGTCTCTTAAGGCACGGTGCTAATTCCATTCAGATCTGATCTGAGAGATAAGAG")
```

## Contributing

Contributions are very welcome.
Expand Down
666 changes: 245 additions & 421 deletions poetry.lock

Large diffs are not rendered by default.

18 changes: 8 additions & 10 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,22 @@ Changelog = "https://github.com/anayden/r2dt-client/releases"

[tool.poetry.dependencies]
python = "^3.9"
click = ">=8.0.1"
ipython = "*"
typer = {extras = ["all"], version = "^0.9.0"}
rich = "^13.7.0"
diskcache = "^5.6.3"
httpx = "^0.25.2"
httpx = "^0.26.0"
yarl = "^1.9.4"
anywidget = {extras = ["dev"], version = "^0.8.0"}
ipython = { version = "^8.18.0", optional = true }
ipywidgets = { version = "^8.1.1", optional = true }

[tool.poetry.scripts]
r2dt-client = "r2dt_client.__main__:main"
[tool.poetry.extras]
widgets = ["ipython", "ipywidget"]

[tool.poetry.group.dev.dependencies]
jupyter = "^1.0.0"
poetry-plugin-export = "^1.6.0"
Pygments = ">=2.10.0"
black = ">=21.10b0"
coverage = {extras = ["toml"], version = ">=6.2"}
coverage = { extras = ["toml"], version = ">=6.2" }
darglint = ">=1.8.1"
flake8 = ">=4.0.1"
flake8-bandit = ">=2.1.2"
Expand All @@ -54,8 +52,8 @@ sphinx = ">=4.3.2"
sphinx-autobuild = ">=2021.3.14"
sphinx-click = ">=3.0.2"
typeguard = ">=2.13.3"
xdoctest = {extras = ["colors"], version = ">=0.15.10"}
myst-parser = {version = ">=0.16.1"}
xdoctest = { extras = ["colors"], version = ">=0.15.10" }
myst-parser = { version = ">=0.16.1" }

[tool.coverage.paths]
source = ["src", "*/site-packages"]
Expand Down
2 changes: 2 additions & 0 deletions src/r2dt_client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from r2dt_client.client.client import setup
from r2dt_client.client.client import submit
from r2dt_client.client.client import update_status_for
from r2dt_client.widgets.widget import draw


__all__ = [
Expand All @@ -13,4 +14,5 @@
"update_status_for",
"fetch_results_for",
"clear_job_cache",
"draw",
]
Empty file.
67 changes: 67 additions & 0 deletions src/r2dt_client/widgets/widget.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import threading
import time


try:
from IPython.display import clear_output
from IPython.display import display
from ipywidgets import ValueWidget

_HAS_IPYWIDGETS = True
except ImportError:
_HAS_IPYWIDGETS = False

from r2dt_client import fetch_results_for
from r2dt_client import submit
from r2dt_client import update_status_for


def _display_spinner() -> ValueWidget:
from ipywidgets import HTML

spinner = HTML(value="<i>Waiting for results from R2DT...</i>")
display(spinner)
return spinner


def _update_spinner(spinner, elapsed_time):
spinner.value = (
f"<i>Waiting for results from R2DT... {elapsed_time} seconds elapsed</i>"
)


def _display_svg(svg_content):
from IPython.display import HTML

display(HTML(svg_content))


def draw(sequence: str) -> None:
if not _HAS_IPYWIDGETS:
print(
"This function is only available in Jupyter notebooks and "
"after installing the library with 'pip install "
"r2dt_client[widgets]'\nConsider using standard API - submit, "
"update_status_for, fetch_results_for - instead."
)
return

job = submit(sequence)

spinner = _display_spinner()
start_time = time.time()

def job_monitor():
while not job.done:
time.sleep(2)
update_status_for(job)
elapsed_time = int(time.time() - start_time)
_update_spinner(spinner, elapsed_time)
fetch_results_for(job)
svg_content = job.results["svg"]
clear_output(wait=True) # Clear the spinner
_display_svg(svg_content)

# Run the monitoring in a separate thread, so it doesn't block the notebook
thread = threading.Thread(target=job_monitor)
thread.start()
16 changes: 2 additions & 14 deletions tests/test_main.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,2 @@
import pytest
from typer.testing import CliRunner

from r2dt_client.__main__ import app


@pytest.fixture
def runner() -> CliRunner:
return CliRunner()


def test_main_succeeds(runner: CliRunner) -> None:
result = runner.invoke(app, ["--help"])
assert result.exit_code == 0
def test_fake() -> None:
pass

0 comments on commit ff121eb

Please sign in to comment.