Skip to content

Commit

Permalink
added boostrap libs to be included
Browse files Browse the repository at this point in the history
  • Loading branch information
Istvan Bozso committed Mar 9, 2020
1 parent 37aa489 commit 6c1e246
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 20 deletions.
1 change: 1 addition & 0 deletions utils/html/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
from utils.html.tag import *
from utils.html.doc import *
from utils.html.jslibs import *
from utils.html.csslibs import *
9 changes: 3 additions & 6 deletions utils/html/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,17 +220,14 @@ def proc_yt_opt(kwargs):

class Library(object):
__slots__ = (
"path",
"path", "options",
)

def __init__(self, path):
def __init__(self, path, *args, **kwargs):
self.path = path

self.options = kwargs

class CSSLib(Library):
pass


class Presentation(HTML):
def slide(self, *args, **kwargs):
kwargs["klass"] = "slide"
Expand Down
29 changes: 29 additions & 0 deletions utils/html/csslibs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from utils import namespace
from utils.html import Library, st

__all__ = (
"css",
)

class CSSLib(Library):
def __init__(self, path, *args, **kwargs):
Library.__init__(self, path, *args, **kwargs)


def add(self, *args, **kwargs):
kwargs.update(self.options)

kwargs["rel"] = "stylesheet"
kwargs["href"] = self.path

return st.link(**kwargs)



css = namespace(
bootstrap = CSSLib(
"https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css",
integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh",
crossorigin="anonymous",
),
)
20 changes: 16 additions & 4 deletions utils/html/doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,23 @@ def __init__(self, **kwargs):
class Doc(Children):
__slots__ = (
"filename", "children", "description", "keywords", "author",
"viewport", "encoder",
"viewport", "encoder", "libs",
)

def __init__(self, *args, filename=None, desc="", keywords=[],
author="", viewport={}, encoder=None, title=""):

self.filename, self.description, self.keywords, self.author, \
self.viewport, self.encoder, self.title = (
self.viewport, self.encoder, self.title, self.libs = (
filename, desc, keywords, author,
Viewport(**viewport), encoder, title
Viewport(**viewport), encoder, title, None
)

self.children = ["<!DOCTYPE html>"] + list(args)

def use(self, *args):
self.libs = args

def meta(self):
kw = self.keywords

Expand All @@ -43,13 +46,22 @@ def meta(self):
else:
kw = ",".join(kw)

l = self.libs

if len(l) == 0:
l = ""
else:
l = t.div(id="libraries")
l(lib.add() for lib in self.libs)

return (
t.title(self.title),
st.meta(charset="utf-8"),
st.meta(name="description", content=self.description),
st.meta(name="keywords", content=kw),
st.meta(name="author", content=self.author),
st.meta(name="viewport", content=self.viewport.parse_options()),
l,
)

def write_to(self, writer):
Expand All @@ -66,4 +78,4 @@ def to_file(self, filename=None):

with open(filename, "w") as f:
self.write_to(f)

23 changes: 13 additions & 10 deletions utils/html/jslibs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,39 @@
from utils.html import Library, t

__all__ = (
"libs", "plotly", "Plotly",
"js", "plotly", "Plotly",
)

class JSLib(Library):
__slots__ = (
"_async",
)

def __init__(self, *args, **kwargs):
def __init__(self, path, *args, **kwargs):
self._async = bool(kwargs.get("async", True))

Library.__init__(self, kwargs["path"])
Library.__init__(self, path, *args, **kwargs)

def add(self, *args, **kwargs):
args = set(args)
def add(self, **kwargs):
kwargs.update(self.options)

if self._async:
args.add("async")
kwargs["async"] = True

kwargs["src"] = self.path

return t.script(**kwargs)


libs = namespace(
js = namespace(
shower=JSLib(path="https://shwr.me/shower/shower.min.js"),
mathjax=JSLib(path="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"),
jquery=JSLib(path="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"),
bootstrap = JSLib(
path="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js",
integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6",
crossorigin="anonymous",
),
)

class Plotly(JSLib):
Expand All @@ -47,9 +52,7 @@ def __init__(self, *args, **kwargs):
else:
path = self.base.format(version=version)

kwargs["path"] = path

JSLib.__init__(self, *args, **kwargs)
JSLib.__init__(self, path, *args, **kwargs)


plotly = namespace(
Expand Down
10 changes: 10 additions & 0 deletions utils/html/tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

__all__ = (
"tags", "t", "stags", "st", "Children", "Options", "url", "doi",
"collapsable",
)


Expand Down Expand Up @@ -155,6 +156,15 @@ def url(address, txt, **kwargs):
def doi(number, **kwargs):
return url("https://doi.org/%s" % number, **kwargs)

def collapsable(name, id, **kwargs):
b = t.button(name, **{
"data-toggle":"collapse",
"data_target":id
})

d = t.div(id=id, klass="collapse")

return b, d

symbols = namespace(
linebreak = "<br>",
Expand Down

0 comments on commit 6c1e246

Please sign in to comment.