mirror of https://github.com/JoshKarpel/spiel
Big cleanup (#89)
parent
240684f5ed
commit
9346e7212b
@ -1,28 +0,0 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
import os
|
||||
|
||||
from rich.console import Console
|
||||
|
||||
from spiel.main import DEMO_SOURCE
|
||||
from spiel.present import render_slide
|
||||
from spiel.state import State
|
||||
|
||||
CYCLES_PER_SLIDE = 100
|
||||
|
||||
|
||||
def render_image_repeatedly() -> None:
|
||||
with open(os.devnull, "w") as f:
|
||||
state = State.from_file(DEMO_SOURCE, console=Console(file=f))
|
||||
|
||||
state.trigger()
|
||||
state.trigger()
|
||||
for _ in range(CYCLES_PER_SLIDE):
|
||||
slide = [slide for slide in state.deck.slides if "Plot" in slide.title][0]
|
||||
rendered = render_slide(state, slide)
|
||||
state.console.print(rendered)
|
||||
state.trigger()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
render_image_repeatedly()
|
File diff suppressed because it is too large
Load Diff
@ -1,9 +1,8 @@
|
||||
from .constants import __version__
|
||||
from .deck import Deck
|
||||
from .example import Example, example_panels
|
||||
from .image import Image
|
||||
from .notebooks import notebook
|
||||
from .options import Options
|
||||
from .repls import repl
|
||||
from .slide import Slide
|
||||
from .triggers import Triggers
|
||||
from spiel.constants import __version__
|
||||
from spiel.deck import Deck
|
||||
from spiel.example import Example, example_panels
|
||||
from spiel.image import Image
|
||||
from spiel.options import Options
|
||||
from spiel.repls import repl
|
||||
from spiel.slide import Slide
|
||||
from spiel.triggers import Triggers
|
||||
|
@ -1,4 +1,4 @@
|
||||
from .constants import PACKAGE_NAME
|
||||
from .main import app
|
||||
from spiel.constants import PACKAGE_NAME
|
||||
from spiel.main import app
|
||||
|
||||
app(prog_name=PACKAGE_NAME)
|
||||
|
@ -1,57 +0,0 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
"Welcome to the demo notebook!\n",
|
||||
"\n",
|
||||
"Not much to see here...\n",
|
||||
"\n",
|
||||
"This is powered by nbterm (https://github.com/davidbrochart/nbterm).\n",
|
||||
"See their `README` for usage."
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false,
|
||||
"pycharm": {
|
||||
"name": "#%% md\n"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"print('foobar')\n",
|
||||
"2 ** 10"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false,
|
||||
"pycharm": {
|
||||
"name": "#%%\n"
|
||||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 2
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython2",
|
||||
"version": "2.7.6"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 0
|
||||
}
|
Before Width: | Height: | Size: 53 KiB After Width: | Height: | Size: 53 KiB |
@ -1,30 +0,0 @@
|
||||
from typing import TYPE_CHECKING, Callable, MutableMapping
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from .state import State
|
||||
|
||||
from nbterm import Notebook
|
||||
|
||||
NotebookExecutor = Callable[["State"], None]
|
||||
|
||||
NOTEBOOKS: MutableMapping[str, NotebookExecutor] = {}
|
||||
|
||||
|
||||
def notebook(name: str) -> Callable[[NotebookExecutor], NotebookExecutor]:
|
||||
def registrar(executor: NotebookExecutor) -> NotebookExecutor:
|
||||
NOTEBOOKS[name] = executor
|
||||
return executor
|
||||
|
||||
return registrar
|
||||
|
||||
|
||||
@notebook("nbterm")
|
||||
def nbterm(state: "State") -> None:
|
||||
save_path = state.tmp_dir / f"{id(state.current_slide)}.ipynb"
|
||||
|
||||
nb = Notebook(state.current_slide.notebook or save_path)
|
||||
|
||||
state.current_slide.notebook = save_path
|
||||
|
||||
nb.show()
|
||||
nb.save(save_path)
|
@ -1,89 +0,0 @@
|
||||
import pickle
|
||||
import re
|
||||
from functools import lru_cache
|
||||
from typing import Any, Iterable, List, Sequence, Union
|
||||
|
||||
import numpy as np
|
||||
import uniplot
|
||||
from colorama import Fore
|
||||
from colorama import Style as CStyle
|
||||
from rich.console import Console, ConsoleOptions
|
||||
from rich.segment import Segment
|
||||
from rich.style import Style
|
||||
|
||||
Plottable = Union[
|
||||
np.ndarray,
|
||||
Sequence[np.ndarray],
|
||||
]
|
||||
|
||||
RE_ANSI_ESCAPE = re.compile(r"(\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~]))")
|
||||
|
||||
ANSI_COLOR_TO_STYLE = {
|
||||
CStyle.RESET_ALL: Style.null(),
|
||||
Fore.RED: Style(color="red"),
|
||||
Fore.GREEN: Style(color="green"),
|
||||
Fore.BLUE: Style(color="blue"),
|
||||
Fore.CYAN: Style(color="cyan"),
|
||||
Fore.YELLOW: Style(color="yellow"),
|
||||
Fore.MAGENTA: Style(color="magenta"),
|
||||
Fore.BLACK: Style(color="black"),
|
||||
Fore.WHITE: Style(color="white"),
|
||||
}
|
||||
|
||||
|
||||
@lru_cache(maxsize=2 ** 8)
|
||||
def _ansi_to_text(s: str) -> List[Segment]:
|
||||
segments = []
|
||||
tmp = ""
|
||||
null_style = Style.null()
|
||||
style = null_style
|
||||
for char in RE_ANSI_ESCAPE.split(s):
|
||||
if char in ANSI_COLOR_TO_STYLE:
|
||||
segments.append(Segment(tmp, style=style))
|
||||
style = ANSI_COLOR_TO_STYLE[char]
|
||||
tmp = ""
|
||||
else:
|
||||
tmp += char
|
||||
|
||||
# catch leftovers
|
||||
segments.append(Segment(tmp, style=style))
|
||||
|
||||
return list(Segment.simplify(segments))
|
||||
|
||||
|
||||
@lru_cache(maxsize=2 ** 8)
|
||||
def _make_plot(pickled_plot_args: bytes) -> List[str]:
|
||||
# This is kind of ugly, but we pickle the args before passing them as an easy
|
||||
# way to make them hashable. This helps a lot for performance on static plots,
|
||||
# and doesn't have toooooo much impact on dynamic plots.
|
||||
return uniplot.plot_to_string(**pickle.loads(pickled_plot_args))
|
||||
|
||||
|
||||
class Plot:
|
||||
def __init__(
|
||||
self,
|
||||
**plot_args: Any,
|
||||
) -> None:
|
||||
self.plot_args = plot_args
|
||||
|
||||
def __rich_console__(self, console: Console, options: ConsoleOptions) -> Iterable[Segment]:
|
||||
if self.plot_args.get("height") is None and options.height is None:
|
||||
height = None
|
||||
else:
|
||||
# 5 = title + top bar + bottom bar + bottom axis labels + 1
|
||||
height = max(
|
||||
(options.height - 5) if options.height else 1, self.plot_args.get("height", 1)
|
||||
)
|
||||
|
||||
plot_args = {
|
||||
**self.plot_args,
|
||||
**dict(
|
||||
height=height,
|
||||
width=max(options.max_width - 10, self.plot_args.get("width", 1)),
|
||||
),
|
||||
}
|
||||
|
||||
plot = "\n".join(_make_plot(pickled_plot_args=pickle.dumps(plot_args)))
|
||||
# plot = "\n".join(uniplot.plot_to_string(plot_args))
|
||||
|
||||
yield from _ansi_to_text(plot)
|
@ -1,17 +1,20 @@
|
||||
from typing import Any, List
|
||||
from typing import List, Optional, TypeVar
|
||||
|
||||
import pytest
|
||||
|
||||
from spiel.utils import chunks
|
||||
|
||||
T = TypeVar("T")
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"items, n, fill, expected",
|
||||
[
|
||||
("abcdef", 3, None, [["a", "b", "c"], ["d", "e", "f"]]),
|
||||
("abcde", 3, None, [["a", "b", "c"], ["d", "e", None]]),
|
||||
("abcde", 3, "fill", [["a", "b", "c"], ["d", "e", "fill"]]),
|
||||
("", 2, None, []),
|
||||
],
|
||||
)
|
||||
def test_chunks(items: List[Any], n: int, fill: Any, expected: List[List[Any]]) -> None:
|
||||
assert [list(chunk) for chunk in chunks(items, n)] == expected
|
||||
def test_chunks(items: List[T], n: int, fill: Optional[T], expected: List[List[T]]) -> None:
|
||||
assert [list(chunk) for chunk in chunks(items, n, fill_value=fill)] == expected
|
||||
|
Loading…
Reference in New Issue