Skip to content

Commit bef2542

Browse files
authored
Merge pull request #43 from apiad/feature/fastapi
Feature/fastapi
2 parents 3bd579a + 72a1756 commit bef2542

15 files changed

+356
-329
lines changed

Readme.md

+11-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
[<img alt="Gitter" src="https://img.shields.io/gitter/room/apiad/auditorium">](https://gitter.im/auditorium-slides/community)
99
[<img alt="Demo" src="https://img.shields.io/badge/demo-browse-blueviolet"></img>](https://auditorium-demo.apiad.net)
1010

11+
<img src="auditorium/static/img/logo.png"></img>
12+
1113
> A Python-powered slideshow creator with steroids.
1214
1315
See the demo at [auditorium-demo.apiad.net](https://auditorium-demo.apiad.net).
@@ -253,7 +255,11 @@ If you have a slideshow to showcase here, feel free to [edit this Readme](https:
253255
If you feel like sending some support please consider adding a badge somewhere in your website or repository:
254256

255257
```html
256-
<a href="https://apiad.net/auditorium"><img alt="Made with Auditorium" src="https://img.shields.io/badge/made--with-auditorium-blue"></img></a>
258+
<a href="https://apiad.net/auditorium">
259+
<img alt="Made with Auditorium"
260+
src="https://img.shields.io/badge/made--with-auditorium-blue">
261+
</img>
262+
</a>
257263
```
258264

259265
It looks like this:
@@ -262,6 +268,10 @@ It looks like this:
262268

263269
## History
264270

271+
### v19.1.2
272+
273+
* Switched to [FastAPI](https://fastapi.tiangolo.com) 🤓🤓 !!
274+
265275
### v19.1.1
266276

267277
* To celebrate the new year we are switching to [calver](https://calver.org/) versioning for good!

auditorium/show.py

+32-16
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import base64
88
import io
99
import runpy
10+
import warnings
1011
import webbrowser
1112
from collections import OrderedDict
1213

@@ -16,14 +17,25 @@
1617
from pygments.formatters.html import HtmlFormatter
1718
from pygments.lexers import get_lexer_by_name
1819
from pygments.styles import get_style_by_name
19-
from sanic import Sanic
20-
from sanic.response import html, json
20+
21+
from fastapi import FastAPI
22+
from starlette.staticfiles import StaticFiles
23+
from starlette.responses import HTMLResponse
24+
from pydantic import BaseModel
25+
from typing import Union
2126

2227
from .components import Animation, Block, Column, Fragment, ShowMode
2328
from .utils import fix_indent, path
2429

2530

26-
class Show:
31+
class UpdateData(BaseModel):
32+
type: str
33+
id: str
34+
slide: str
35+
value: Union[int, str]
36+
37+
38+
class Show(FastAPI):
2739
def __init__(self, title="", theme="white", code_style="monokai"):
2840
self.theme = theme
2941
self.formatter = HtmlFormatter(style=get_style_by_name(code_style))
@@ -32,10 +44,10 @@ def __init__(self, title="", theme="white", code_style="monokai"):
3244
self._sections = []
3345
self._tail = []
3446

35-
self.app = Sanic("auditorium")
36-
self.app.route("/")(self._index)
37-
self.app.route("/update", methods=["POST"])(self._update)
38-
self.app.static("static", path("static"))
47+
self.app = FastAPI()
48+
self.app.get("/")(self._index)
49+
self.app.post("/update")(self._update)
50+
self.app.mount("/static", StaticFiles(directory=path("static")))
3951

4052
self._title = title
4153
self._current_section = None
@@ -61,7 +73,13 @@ def run(self, host: str, port: int, launch: bool, *args, **kwargs) -> None:
6173

6274
# self.app.add_task(launch_server)
6375

64-
self.app.run(host=host, port=port, *args, **kwargs)
76+
try:
77+
import uvicorn
78+
79+
uvicorn.run(self.app, host=host, port=port, *args, **kwargs)
80+
except ImportError:
81+
warnings.warn("In order to call `run` you need `uvicorn` installed.")
82+
exit(1)
6583

6684
@property
6785
def show_title(self) -> str:
@@ -192,16 +210,14 @@ def _code_style(self):
192210

193211
## Routes
194212

195-
async def _update(self, request):
196-
data = request.json
213+
async def _update(self, data: UpdateData):
197214
values = {}
198-
values[data["id"]] = data["value"]
199-
update = self.do_code(data["slide"], values)
200-
return json(update)
215+
values[data.id] = data.value
216+
update = self.do_code(data.slide, values)
217+
return update
201218

202-
async def _index(self, request):
203-
theme = request.args.get("theme", self.theme)
204-
return html(
219+
async def _index(self, theme: str = "white"):
220+
return HTMLResponse(
205221
self._template_dynamic.render(
206222
show=self,
207223
content=self._content,

auditorium/static/img/favicon.ico

30.7 KB
Binary file not shown.

auditorium/static/img/logo.png

44.9 KB
Loading

auditorium/static/img/logo.svg

+83
Loading

demo/api/update.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -342,8 +342,16 @@ def pyplot(ctx):
342342
Dynamically generated graphs with `pyplot` can be added
343343
also very easily.
344344
"""
345-
from matplotlib import pyplot as plt
346-
import numpy as np
345+
346+
try:
347+
from matplotlib import pyplot as plt
348+
import numpy as np
349+
except ImportError:
350+
with ctx.error("Dependencies missing"):
351+
ctx.markdown("You need `matplotlib` installed to make this slide work. Make sure to run:")
352+
353+
ctx.code("pip install matplotlib", "bash")
354+
return
347355

348356
xg = np.random.RandomState(0)
349357
yg = np.random.RandomState(1)

demo/index.html

+4-4
Original file line numberDiff line numberDiff line change
@@ -2344,7 +2344,7 @@ <h3>.</h3>
23442344
They can be skipped on shorter presentations and left
23452345
for more interested audiences.</p></div>
23462346

2347-
<div class="block block-warning"><h1 class="block-title"><auditorium.show.Context object at 0x7f3f50389340></h1><div class="block-content">
2347+
<div class="block block-warning"><h1 class="block-title"><auditorium.show.Context object at 0x7f842be770d0></h1><div class="block-content">
23482348

23492349
<div id="vertical_slides-markdown-1" data-slide="vertical_slides"><p>Press <code>DOWN</code> instead of <code>LEFT</code> or click the down arrow.</p></div>
23502350

@@ -2457,13 +2457,13 @@ <h3>.</h3>
24572457
<div id="fragments-markdown-0" data-slide="fragments"><h2>Fragments</h2>
24582458
<p>Fragments allow to animate elements inside a slide.</p></div>
24592459

2460-
<div class="fragment <auditorium.show.Context object at 0x7f3f50389340>">
2460+
<div class="fragment <auditorium.show.Context object at 0x7f842be770d0>">
24612461

24622462
<div id="fragments-markdown-1" data-slide="fragments"><p>The can have different animations as well...</p></div>
24632463

24642464
</div>
24652465

2466-
<div class="fragment <auditorium.show.Context object at 0x7f3f50389340>">
2466+
<div class="fragment <auditorium.show.Context object at 0x7f842be770d0>">
24672467

24682468
<div id="fragments-markup-2" data-slide="fragments"><hr></div>
24692469

@@ -2661,7 +2661,7 @@ <h3>.</h3>
26612661

26622662
<div id="themes-markdown-3" data-slide="themes"><p>Or directly as a query string arg:</p></div>
26632663

2664-
<div class="block block-default"><h1 class="block-title"><auditorium.show.Context object at 0x7f3f50389340></h1><div class="block-content">
2664+
<div class="block block-default"><h1 class="block-title"><auditorium.show.Context object at 0x7f842be770d0></h1><div class="block-content">
26652665

26662666
<a id="themes-anchor-4" data-slide="themes" href="/?theme=black#/themes">/?theme=black#/themes</a>
26672667

demo/requirements.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
auditorium==0.6.5
1+
auditorium==19.1.2
22
matplotlib==3.1.2

docker-compose.yml

+24-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
version: "3"
22

33
services:
4-
auditorium-tester:
5-
container_name: auditorium-tester-${PYTHON_VERSION}
6-
image: docker.pkg.github.com/apiad/auditorium/auditorium:${PYTHON_VERSION}
4+
auditorium-dev:
5+
container_name: auditorium-dev-${PYTHON_VERSION}
6+
image: auditorium/auditorium-dev:${PYTHON_VERSION}
77
build:
88
context: "."
9+
dockerfile: "docker/dev.dockerfile"
910
args:
1011
PYTHON_VERSION: ${PYTHON_VERSION}
1112
volumes:
@@ -15,3 +16,23 @@ services:
1516
network_mode: host
1617
environment:
1718
- "NOW_TOKEN=${NOW_TOKEN}"
19+
20+
auditorium:
21+
container_name: auditorium
22+
image: auditorium/auditorium:${AUDITORIUM_VERSION}
23+
build:
24+
context: "."
25+
dockerfile: "docker/dockerfile"
26+
args:
27+
AUDITORIUM_VERSION: ${AUDITORIUM_VERSION}
28+
network_mode: host
29+
30+
auditorium-serverless:
31+
container_name: auditorium-serverless
32+
image: auditorium/auditorium-serverless:${AUDITORIUM_VERSION}
33+
build:
34+
context: "."
35+
dockerfile: "docker/serverless.dockerfile"
36+
args:
37+
AUDITORIUM_VERSION: ${AUDITORIUM_VERSION}
38+
network_mode: host

dockerfile renamed to docker/dev.dockerfile

+2-4
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,11 @@ ARG PYTHON_VERSION
66
FROM python:${PYTHON_VERSION}
77

88
# Install yarn and now
9-
RUN if [ "${PYTHON_VERSION}" == "3.8" ] ; then \
10-
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - && \
9+
RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - && \
1110
echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list && \
1211
apt update && \
1312
apt install -y yarn && \
14-
yarn global add now ; \
15-
fi
13+
yarn global add now
1614

1715
# ==========================================
1816
# Project-specific installation instruction

docker/dockerfile

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
FROM python:3.8
2+
3+
ARG AUDITORIUM_VERSION
4+
COPY dist/auditorium-${AUDITORIUM_VERSION}-py3-none-any.whl /dist/auditorium-${AUDITORIUM_VERSION}-py3-none-any.whl
5+
RUN pip install --no-cache-dir /dist/auditorium-${AUDITORIUM_VERSION}-py3-none-any.whl[server]

docker/serverless.dockerfile

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
FROM python:3.8-alpine
2+
3+
ARG AUDITORIUM_VERSION
4+
COPY dist/auditorium-${AUDITORIUM_VERSION}-py3-none-any.whl /dist/auditorium-${AUDITORIUM_VERSION}-py3-none-any.whl
5+
RUN pip install --no-cache-dir /dist/auditorium-${AUDITORIUM_VERSION}-py3-none-any.whl

makefile

+8-8
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,31 @@ BASE_VERSION := 3.8
44
ALL_VERSIONS := 3.6 3.7 3.8
55

66
test-fast:
7-
PYTHON_VERSION=${BASE_VERSION} docker-compose run auditorium-tester make dev-test-fast
7+
PYTHON_VERSION=${BASE_VERSION} docker-compose run auditorium-dev make dev-test-fast
88

99
docs:
10-
PYTHON_VERSION=${BASE_VERSION} docker-compose run auditorium-tester mkdocs serve
10+
PYTHON_VERSION=${BASE_VERSION} docker-compose run auditorium-dev mkdocs serve
1111

1212
shell:
13-
PYTHON_VERSION=${BASE_VERSION} docker-compose run auditorium-tester bash
13+
PYTHON_VERSION=${BASE_VERSION} docker-compose run auditorium-dev bash
1414

1515
lock:
16-
PYTHON_VERSION=${BASE_VERSION} docker-compose run auditorium-tester poetry lock
16+
PYTHON_VERSION=${BASE_VERSION} docker-compose run auditorium-dev poetry lock
1717

1818
build:
19-
PYTHON_VERSION=${BASE_VERSION} docker-compose run auditorium-tester poetry build
19+
PYTHON_VERSION=${BASE_VERSION} docker-compose run auditorium-dev poetry build
2020

2121
clean:
2222
git clean -fxd
2323

2424
lint:
25-
PYTHON_VERSION=${BASE_VERSION} docker-compose run auditorium-tester poetry run pylint auditorium
25+
PYTHON_VERSION=${BASE_VERSION} docker-compose run auditorium-dev poetry run pylint auditorium
2626

2727
test-full:
2828
$(foreach VERSION, $(ALL_VERSIONS), PYTHON_VERSION=${VERSION} docker-compose up;)
2929

3030
docker-build:
31-
$(foreach VERSION, $(ALL_VERSIONS), PYTHON_VERSION=${VERSION} docker-compose build;)
31+
$(foreach VERSION, $(ALL_VERSIONS), PYTHON_VERSION=${VERSION} docker-compose build auditorium-dev;)
3232

3333
docker-push:
3434
$(foreach VERSION, $(ALL_VERSIONS), PYTHON_VERSION=${VERSION} docker-compose push;)
@@ -45,7 +45,7 @@ dev-ensure:
4545
dev-install: dev-ensure
4646
pip install poetry
4747
poetry config virtualenvs.create false
48-
poetry install
48+
poetry install -E server
4949

5050
dev-test-fast: dev-ensure
5151
python -m mypy -p auditorium --ignore-missing-imports

0 commit comments

Comments
 (0)