Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Maintenance #24

Merged
merged 6 commits into from
Oct 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 44 additions & 45 deletions .github/workflows/pythonpackage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,65 +3,64 @@ name: build
on:
pull_request:
paths:
- '**'
- "**"

push:
paths:
- '**'
- '!notebooks/**'
- '!scripts/**'
- '!binder/**'
- "**"
- "!notebooks/**"
- "!scripts/**"
- "!binder/**"

jobs:
build:

runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v3

- name: Set up Python
uses: actions/setup-python@v1
with:
python-version: '3.8'
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: "3.8"

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install flake8 wheel twine
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install flake8 wheel twine

- name: Lint with flake8
run: |
pip install flake8
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- name: Lint with flake8
run: |
pip install flake8
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics

- name: Package wheel
run: |
pip install wheel
python setup.py bdist_wheel
find dist -name "*whl" -exec unzip -l '{}' \;
- name: Package wheel
run: |
pip install wheel
python setup.py bdist_wheel
find dist -name "*whl" -exec unzip -l '{}' \;

- name: Package source distribution
run: |
python setup.py sdist
- name: Package source distribution
run: |
python setup.py sdist

- name: Check Distribution Files
run: |
twine check dist/*
- name: Check Distribution Files
run: |
twine check dist/*

- name: Publish (test pypi)
if: |
github.event_name == 'push' && (
github.ref == 'refs/heads/develop'
|| github.ref == 'refs/heads/prep-release'
)
- name: Publish (test pypi)
if: |
github.event_name == 'push' && (
github.ref == 'refs/heads/develop'
|| github.ref == 'refs/heads/prep-release'
)

run: |
twine upload --verbose --disable-progress-bar --skip-existing dist/* || true
run: |
twine upload --verbose --disable-progress-bar --skip-existing dist/* || true

env:
TWINE_USERNAME: '__token__'
TWINE_PASSWORD: ${{ secrets.test_pypi_token }}
TWINE_REPOSITORY_URL: 'https://test.pypi.org/legacy/'
env:
TWINE_USERNAME: "__token__"
TWINE_PASSWORD: ${{ secrets.test_pypi_token }}
TWINE_REPOSITORY_URL: "https://test.pypi.org/legacy/"
7 changes: 4 additions & 3 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
#
import os
import sys
sys.path.insert(0, os.path.abspath('..'))

sys.path.insert(0, os.path.abspath(".."))
from jupyter_ui_poll import __version__


Expand Down Expand Up @@ -84,8 +85,8 @@
"logo_only": True,
}

#html_logo = '_static/logo.svg'
html_last_updated_fmt = '%b %d, %Y'
# html_logo = '_static/logo.svg'
html_last_updated_fmt = "%b %d, %Y"
html_show_sphinx = False


Expand Down
32 changes: 16 additions & 16 deletions jupyter_ui_poll/_poll.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import time
from collections import abc
from functools import singledispatch
from inspect import iscoroutinefunction, isawaitable
from inspect import isawaitable, iscoroutinefunction
from typing import (
Any,
AsyncIterable,
Expand All @@ -16,17 +16,16 @@
Optional,
Tuple,
TypeVar,
Union,
)

import zmq
from IPython import get_ipython
from tornado.queues import QueueEmpty

from ._async_thread import AsyncThread

T = TypeVar("T")

ZMQ_POLLOUT = 2 # zmq.POLLOUT without zmq dependency


class KernelWrapper:
_current: Optional["KernelWrapper"] = None
Expand All @@ -53,8 +52,7 @@ def __init__(self, shell, loop) -> None:
# ipykernel < 6
kernel.shell_handlers["execute_request"] = self._execute_request

shell.events.register("post_run_cell", self._post_run_cell_hook)
shell.events.register("post_execute", self._post_run_cell_hook)
shell.events.register("post_execute", self._post_execute_hook)

def restore(self):
if self._backup_execute_request is not None:
Expand Down Expand Up @@ -98,7 +96,7 @@ async def replay(self):
sys.stderr.flush()
if shell_stream is not None: # 6+
kernel._publish_status("idle", "shell")
shell_stream.flush(zmq.POLLOUT)
shell_stream.flush(ZMQ_POLLOUT)
else:
kernel._publish_status("idle")

Expand All @@ -107,17 +105,16 @@ async def do_one_iteration(self):
rr = self._kernel.do_one_iteration()
if isawaitable(rr):
await rr
except QueueEmpty:
except Exception: # pylint: disable=broad-except
# it's probably a bug in ipykernel,
# .do_one_iteration() should not throw
return
finally:
# reset stdio back to original cell
self._reset_output()

def _post_run_cell_hook(self, *args, **kw):
self._shell.events.unregister("post_run_cell", self._post_run_cell_hook)
self._shell.events.unregister("post_execute", self._post_run_cell_hook)
def _post_execute_hook(self, *args, **kw):
self._shell.events.unregister("post_execute", self._post_execute_hook)
self.restore()
KernelWrapper._current = None
asyncio.ensure_future(self.replay(), loop=self._loop)
Expand Down Expand Up @@ -153,7 +150,9 @@ def get() -> "KernelWrapper":

class IteratorWrapperAsync(abc.AsyncIterable, Generic[T]):
def __init__(
self, its: AsyncIterable[T], n: int = 1,
self,
its: AsyncIterable[T],
n: int = 1,
):
self._its = its
self._n = n
Expand All @@ -172,7 +171,9 @@ async def _loop(

class IteratorWrapper(abc.Iterable, Generic[T]):
def __init__(
self, its: Iterable[T], n: int = 1,
self,
its: Iterable[T],
n: int = 1,
):
self._its = its
self._n = n
Expand Down Expand Up @@ -324,7 +325,6 @@ def as_iterator(

for x in with_ui_events(as_iterator(f, sleep), n):
if x is not None:
break
return x

assert x is not None
return x
raise RuntimeError("hm...") # for mypy sake
2 changes: 1 addition & 1 deletion jupyter_ui_poll/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.2.1"
__version__ = "0.2.2"
12 changes: 7 additions & 5 deletions notebooks/ComplexUIExample.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"from ipywidgets import HTML\n",
"from ui import blocking_ui\n",
"\n",
"color, mode = blocking_ui(default='beige', timeout=10)"
"color, mode = blocking_ui(default=\"beige\", timeout=10)"
]
},
{
Expand All @@ -32,18 +32,20 @@
"metadata": {},
"outputs": [],
"source": [
"if mode == 'user':\n",
"if mode == \"user\":\n",
" print(f\"So you picked '{color}'\")\n",
"else:\n",
" print('Try to click faster next time')\n",
" print(\"Try to click faster next time\")\n",
"\n",
"HTML(f'''\n",
"HTML(\n",
" f\"\"\"\n",
"<div style=\"width:100px;\n",
" height:100px;\n",
" background:{color};\n",
" padding:10px;\n",
" border-color:black;\n",
" border-style:solid\"><b>{color}</b></div>''')"
" border-style:solid\"><b>{color}</b></div>\"\"\"\n",
")"
]
},
{
Expand Down
28 changes: 14 additions & 14 deletions notebooks/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,27 +55,27 @@ def mk_btn(color):


def blocking_ui(default="beige", timeout=10):
""" Displays a UI then blocks until user makes a choice or timeout happens.
"""Displays a UI then blocks until user makes a choice or timeout happens.

Returns
=======
(color, 'user') if user selects a color in time
(default, 'timeout') in case of a timeout
Returns
=======
(color, 'user') if user selects a color in time
(default, 'timeout') in case of a timeout
"""
state = make_sample_ui()

def poll_cbk():
""" This function is called periodically.
"""This function is called periodically.

- Check for user input
- Check for timeout
- Update timeout progress bar
- Check for user input
- Check for timeout
- Update timeout progress bar

Returns
-------
(color: str, 'user') -- when user selection detected
(default: str, 'timeout') -- when no user selection for too long
None -- in all other cases
Returns
-------
(color: str, 'user') -- when user selection detected
(default: str, 'timeout') -- when no user selection for too long
None -- in all other cases
"""
if state.color is not None: # User selected some color
return state.color, "user"
Expand Down
2 changes: 0 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ setup_requires =
setuptools
install_requires =
ipython
tornado
pyzmq


[options.extras_require]
Expand Down