Skip to content

Commit

Permalink
Merge pull request #43 from apiad/feature/fastapi
Browse files Browse the repository at this point in the history
Feature/fastapi
  • Loading branch information
apiad authored Jan 1, 2020
2 parents 3bd579a + 72a1756 commit bef2542
Show file tree
Hide file tree
Showing 15 changed files with 356 additions and 329 deletions.
12 changes: 11 additions & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
[<img alt="Gitter" src="https://img.shields.io/gitter/room/apiad/auditorium">](https://gitter.im/auditorium-slides/community)
[<img alt="Demo" src="https://img.shields.io/badge/demo-browse-blueviolet"></img>](https://auditorium-demo.apiad.net)

<img src="auditorium/static/img/logo.png"></img>

> A Python-powered slideshow creator with steroids.
See the demo at [auditorium-demo.apiad.net](https://auditorium-demo.apiad.net).
Expand Down Expand Up @@ -253,7 +255,11 @@ If you have a slideshow to showcase here, feel free to [edit this Readme](https:
If you feel like sending some support please consider adding a badge somewhere in your website or repository:

```html
<a href="https://apiad.net/auditorium"><img alt="Made with Auditorium" src="https://img.shields.io/badge/made--with-auditorium-blue"></img></a>
<a href="https://apiad.net/auditorium">
<img alt="Made with Auditorium"
src="https://img.shields.io/badge/made--with-auditorium-blue">
</img>
</a>
```

It looks like this:
Expand All @@ -262,6 +268,10 @@ It looks like this:

## History

### v19.1.2

* Switched to [FastAPI](https://fastapi.tiangolo.com) 🤓🤓 !!

### v19.1.1

* To celebrate the new year we are switching to [calver](https://calver.org/) versioning for good!
Expand Down
48 changes: 32 additions & 16 deletions auditorium/show.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import base64
import io
import runpy
import warnings
import webbrowser
from collections import OrderedDict

Expand All @@ -16,14 +17,25 @@
from pygments.formatters.html import HtmlFormatter
from pygments.lexers import get_lexer_by_name
from pygments.styles import get_style_by_name
from sanic import Sanic
from sanic.response import html, json

from fastapi import FastAPI
from starlette.staticfiles import StaticFiles
from starlette.responses import HTMLResponse
from pydantic import BaseModel
from typing import Union

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


class Show:
class UpdateData(BaseModel):
type: str
id: str
slide: str
value: Union[int, str]


class Show(FastAPI):
def __init__(self, title="", theme="white", code_style="monokai"):
self.theme = theme
self.formatter = HtmlFormatter(style=get_style_by_name(code_style))
Expand All @@ -32,10 +44,10 @@ def __init__(self, title="", theme="white", code_style="monokai"):
self._sections = []
self._tail = []

self.app = Sanic("auditorium")
self.app.route("/")(self._index)
self.app.route("/update", methods=["POST"])(self._update)
self.app.static("static", path("static"))
self.app = FastAPI()
self.app.get("/")(self._index)
self.app.post("/update")(self._update)
self.app.mount("/static", StaticFiles(directory=path("static")))

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

# self.app.add_task(launch_server)

self.app.run(host=host, port=port, *args, **kwargs)
try:
import uvicorn

uvicorn.run(self.app, host=host, port=port, *args, **kwargs)
except ImportError:
warnings.warn("In order to call `run` you need `uvicorn` installed.")
exit(1)

@property
def show_title(self) -> str:
Expand Down Expand Up @@ -192,16 +210,14 @@ def _code_style(self):

## Routes

async def _update(self, request):
data = request.json
async def _update(self, data: UpdateData):
values = {}
values[data["id"]] = data["value"]
update = self.do_code(data["slide"], values)
return json(update)
values[data.id] = data.value
update = self.do_code(data.slide, values)
return update

async def _index(self, request):
theme = request.args.get("theme", self.theme)
return html(
async def _index(self, theme: str = "white"):
return HTMLResponse(
self._template_dynamic.render(
show=self,
content=self._content,
Expand Down
Binary file added auditorium/static/img/favicon.ico
Binary file not shown.
Binary file added auditorium/static/img/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
83 changes: 83 additions & 0 deletions auditorium/static/img/logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 10 additions & 2 deletions demo/api/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,8 +342,16 @@ def pyplot(ctx):
Dynamically generated graphs with `pyplot` can be added
also very easily.
"""
from matplotlib import pyplot as plt
import numpy as np

try:
from matplotlib import pyplot as plt
import numpy as np
except ImportError:
with ctx.error("Dependencies missing"):
ctx.markdown("You need `matplotlib` installed to make this slide work. Make sure to run:")

ctx.code("pip install matplotlib", "bash")
return

xg = np.random.RandomState(0)
yg = np.random.RandomState(1)
Expand Down
8 changes: 4 additions & 4 deletions demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2344,7 +2344,7 @@ <h3>.</h3>
They can be skipped on shorter presentations and left
for more interested audiences.</p></div>

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

<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>

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

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

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

</div>

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

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

Expand Down Expand Up @@ -2661,7 +2661,7 @@ <h3>.</h3>

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

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

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

Expand Down
2 changes: 1 addition & 1 deletion demo/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
auditorium==0.6.5
auditorium==19.1.2
matplotlib==3.1.2
27 changes: 24 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
version: "3"

services:
auditorium-tester:
container_name: auditorium-tester-${PYTHON_VERSION}
image: docker.pkg.github.com/apiad/auditorium/auditorium:${PYTHON_VERSION}
auditorium-dev:
container_name: auditorium-dev-${PYTHON_VERSION}
image: auditorium/auditorium-dev:${PYTHON_VERSION}
build:
context: "."
dockerfile: "docker/dev.dockerfile"
args:
PYTHON_VERSION: ${PYTHON_VERSION}
volumes:
Expand All @@ -15,3 +16,23 @@ services:
network_mode: host
environment:
- "NOW_TOKEN=${NOW_TOKEN}"

auditorium:
container_name: auditorium
image: auditorium/auditorium:${AUDITORIUM_VERSION}
build:
context: "."
dockerfile: "docker/dockerfile"
args:
AUDITORIUM_VERSION: ${AUDITORIUM_VERSION}
network_mode: host

auditorium-serverless:
container_name: auditorium-serverless
image: auditorium/auditorium-serverless:${AUDITORIUM_VERSION}
build:
context: "."
dockerfile: "docker/serverless.dockerfile"
args:
AUDITORIUM_VERSION: ${AUDITORIUM_VERSION}
network_mode: host
6 changes: 2 additions & 4 deletions dockerfile → docker/dev.dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@ ARG PYTHON_VERSION
FROM python:${PYTHON_VERSION}

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

# ==========================================
# Project-specific installation instruction
Expand Down
5 changes: 5 additions & 0 deletions docker/dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FROM python:3.8

ARG AUDITORIUM_VERSION
COPY dist/auditorium-${AUDITORIUM_VERSION}-py3-none-any.whl /dist/auditorium-${AUDITORIUM_VERSION}-py3-none-any.whl
RUN pip install --no-cache-dir /dist/auditorium-${AUDITORIUM_VERSION}-py3-none-any.whl[server]
5 changes: 5 additions & 0 deletions docker/serverless.dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FROM python:3.8-alpine

ARG AUDITORIUM_VERSION
COPY dist/auditorium-${AUDITORIUM_VERSION}-py3-none-any.whl /dist/auditorium-${AUDITORIUM_VERSION}-py3-none-any.whl
RUN pip install --no-cache-dir /dist/auditorium-${AUDITORIUM_VERSION}-py3-none-any.whl
16 changes: 8 additions & 8 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,31 @@ BASE_VERSION := 3.8
ALL_VERSIONS := 3.6 3.7 3.8

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

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

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

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

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

clean:
git clean -fxd

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

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

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

docker-push:
$(foreach VERSION, $(ALL_VERSIONS), PYTHON_VERSION=${VERSION} docker-compose push;)
Expand All @@ -45,7 +45,7 @@ dev-ensure:
dev-install: dev-ensure
pip install poetry
poetry config virtualenvs.create false
poetry install
poetry install -E server

dev-test-fast: dev-ensure
python -m mypy -p auditorium --ignore-missing-imports
Expand Down
Loading

0 comments on commit bef2542

Please sign in to comment.