Use source code for demo directly in demo (#194)

Co-authored-by: Josh Karpel <josh.karpel@gmail.com>
pull/199/head
Ed Rogers 1 year ago committed by GitHub
parent 7a559407af
commit 0f3ece556c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

File diff suppressed because one or more lines are too long

Before

Width:  |  Height:  |  Size: 106 KiB

After

Width:  |  Height:  |  Size: 105 KiB

@ -7,6 +7,11 @@
### Added
- [#185](https://github.com/JoshKarpel/spiel/pull/185) The docs page now includes copy-to-clipboard buttons on all code snippets.
- [#194](https://github.com/JoshKarpel/spiel/pull/194) The demo slides now render their own source code directly to demo bindings functionality.
### Changed
- [#194](https://github.com/JoshKarpel/spiel/pull/194) The `Deck.slide` decorator now returns the decorated function, not the `Slide` it was attached to.
## `0.4.3` | 2023-01-02

@ -54,7 +54,6 @@ plugins:
- https://rich.readthedocs.io/en/stable/objects.inv
- https://textual.textualize.io/objects.inv
markdown_extensions:
- admonition
- pymdownx.details

@ -22,7 +22,7 @@ class Deck(Sequence[Slide]):
self,
title: str = "",
bindings: Mapping[str, Callable[..., None]] | None = None,
) -> Callable[[Content], Slide]:
) -> Callable[[Content], Content]:
"""
A decorator that creates a new slide in the deck,
with the decorated function as the `Slide`'s `content`.
@ -35,14 +35,15 @@ class Deck(Sequence[Slide]):
when on this slide.
"""
def slideify(content: Content) -> Slide:
slide = Slide(
title=title,
content=content,
bindings=bindings or {},
def slideify(content: Content) -> Content:
self.add_slides(
Slide(
title=title,
content=content,
bindings=bindings or {},
)
)
self.add_slides(slide)
return slide
return content
return slideify

@ -6,7 +6,7 @@ import socket
from datetime import datetime
from math import cos, floor, pi
from pathlib import Path
from textwrap import dedent
from textwrap import dedent, indent
from click import edit
from rich.align import Align
@ -375,7 +375,18 @@ def edit_this_file(suspend: SuspendType) -> None:
},
)
def bindings() -> RenderableType:
edit_function_src = dedent("".join(inspect.getsourcelines(edit_this_file)[0]))
edit_this_file_source_lines, _ = inspect.getsourcelines(edit_this_file)
this_slide_source_lines, _ = inspect.getsourcelines(bindings)
source_lines = [
*edit_this_file_source_lines,
"\n", # blank line between the two functions
*this_slide_source_lines[:7], # up through the slide's def
" ...", # replace the slide body with a "..."
]
# get the indentation right for the triple-quoted string below
source_code = indent("".join(source_lines), " " * 8).lstrip()
return pad_markdown(
f"""\
## Custom Per-Slide Key Bindings
@ -383,20 +394,6 @@ def bindings() -> RenderableType:
Custom keybindings can be added on a per-slide basis using the `bindings` argument of `@slide`,
which takes a mapping of key names to callables to call when that key is pressed.
```python
def edit_this_file(suspend: SuspendType) -> None:
with suspend():
edit(filename=__file__)
@deck.slide(
title="Bindings",
bindings={{
"e": edit_this_file,
}},
)
def bindings() -> RenderableType:
...
```
If the callable takes an argument named `suspend`,
it will be passed a function that, when used as a context manager,
@ -406,6 +403,10 @@ def bindings() -> RenderableType:
and opens your `$EDITOR` on the demo deck's Python file.
Try pressing `e`!
```python
{source_code}
```
Due to reloading, any changes you make will be reflected in the
presentation you're seeing right now.
"""

Loading…
Cancel
Save