-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
some cleanup around the root template (#9)
* some cleanup around the root template * bump version
- Loading branch information
Showing
6 changed files
with
89 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
.vscode/ | ||
node_modules/ | ||
_site/ | ||
# Byte-compiled / optimized / DLL files | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
from fastapi.staticfiles import StaticFiles | ||
from starlette.routing import Route | ||
from pyview import PyView, defaultRootTemplate | ||
from markupsafe import Markup | ||
|
||
from .views import ( | ||
CountLiveView, | ||
|
@@ -24,7 +25,12 @@ | |
<link rel="stylesheet" href="https://unpkg.com/[email protected]/nprogress.css" /> | ||
""" | ||
|
||
app.rootTemplate = defaultRootTemplate(css) | ||
|
||
def content_wrapper(_context, content: Markup) -> Markup: | ||
return Markup("<a href='/'>Home</a>") + content | ||
|
||
|
||
app.rootTemplate = defaultRootTemplate(css=Markup(css), content_wrapper=content_wrapper) | ||
|
||
routes = [ | ||
( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ packages = [ | |
{ include = "pyview" }, | ||
] | ||
|
||
version = "0.0.7a" | ||
version = "0.0.8a" | ||
description = "LiveView in Python" | ||
authors = ["Larry Ogrodnek <[email protected]>"] | ||
license = "MIT" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
from pyview.vendor.ibis import Template | ||
from .live_template import LiveTemplate, template_file, RenderedContent, LiveRender | ||
from .root_template import RootTemplate, RootTemplateContext, defaultRootTemplate |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
from typing import Callable, Optional, TypedDict | ||
from markupsafe import Markup | ||
|
||
|
||
class RootTemplateContext(TypedDict): | ||
id: str | ||
content: str | ||
title: Optional[str] | ||
csrf_token: str | ||
session: Optional[str] | ||
|
||
|
||
RootTemplate = Callable[[RootTemplateContext], str] | ||
ContentWrapper = Callable[[RootTemplateContext, Markup], Markup] | ||
|
||
|
||
def defaultRootTemplate( | ||
css: Optional[Markup] = None, content_wrapper: Optional[ContentWrapper] = None | ||
) -> RootTemplate: | ||
content_wrapper = content_wrapper or (lambda c, m: m) | ||
|
||
def template(context: RootTemplateContext) -> str: | ||
return _defaultRootTemplate(context, css or Markup(""), content_wrapper) | ||
|
||
return template | ||
|
||
|
||
def _defaultRootTemplate( | ||
context: RootTemplateContext, css: Markup, contentWrapper: ContentWrapper | ||
) -> str: | ||
suffix = " | LiveView" | ||
render_title = (context["title"] + suffix) if context.get("title", None) is not None else "LiveView" # type: ignore | ||
main_content = contentWrapper( | ||
context, | ||
Markup( | ||
f""" | ||
<div | ||
data-phx-main="true" | ||
data-phx-session="{context['session']}" | ||
data-phx-static="" | ||
id="phx-{context['id']}" | ||
> | ||
{context['content']} | ||
</div>""" | ||
), | ||
) | ||
|
||
return ( | ||
Markup( | ||
f""" | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<title data-suffix="{suffix}">{render_title}</title> | ||
<meta name="csrf-token" content="{context['csrf_token']}" /> | ||
<meta charset="utf-8"> | ||
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> | ||
<script defer type="text/javascript" src="/static/assets/app.js"></script> | ||
{css} | ||
</head> | ||
<body>""" | ||
) | ||
+ main_content | ||
+ Markup( | ||
""" | ||
</body> | ||
</html> | ||
""" | ||
) | ||
) |