from IPython.display import Markdown
from pprint import pprint
XML
FT
FT (tag:str, cs:tuple, attrs:dict=None, void_=False, **kwargs)
A ‘Fast Tag’ structure, containing tag
,children
,and attrs
attrmap
attrmap (o)
valmap
valmap (o)
ft
ft (tag:str, *c, void_:bool=False, attrmap:<built- infunctioncallable>=<function attrmap>, valmap:<built- infunctioncallable>=<function valmap>, **kw)
Create an FT
structure for to_xml()
= Body(Div('hi', a=1, b=True, cls=()), P('hi', cls=['a',1], style=dict(a=1,b=2)))
a a
body((div(('hi',),{'a': 1, 'b': True, 'class': None}), p(('hi',),{'class': 'a 1', 'style': 'a:1; b:2'})),{})
+ (P('a'),P('b')) a
body((div(('hi',),{'a': 1, 'b': True, 'class': None}), p(('hi',),{'class': 'a 1', 'style': 'a:1; b:2'}), p(('a',),{}), p(('b',),{})),{})
The main HTML tags are exported as ft
partials.
Attributes are passed as keywords. Use ‘klass’ and ‘fr’ instead of ‘class’ and ‘for’, to avoid Python reserved word clashes.
Html
Html (*c, doctype=True, **kwargs)
An HTML tag, optionally preceeded by !DOCTYPE HTML
= Html(
samp 'Some page')),
Head(Title('Some text\nanother line', (Input(name="jph's"), Img(src="filename", data=1)),
Body(Div(=['myclass', 'another'],
cls={'padding':1, 'margin':2}))
style
) pprint(samp)
(!doctype((),{'html': True}),
html((head((title(('Some page',),{}),),{}), body((div(('Some text\nanother line', input((),{'name': "jph's"}), img((),{'src': 'filename', 'data': 1})),{'class': 'myclass another', 'style': 'padding:1; margin:2'}),),{})),{}))
= P('Some text', id="myid")
elem print(elem.tag)
print(elem.children)
print(elem.attrs)
p
('Some text',)
{'id': 'myid'}
You can get and set attrs directly:
id = 'newid'
elem.print(elem.id, elem.get('id'), elem.get('foo', 'missing'))
elem
newid newid missing
p(('Some text',),{'id': 'newid'})
Safe
*str(object=’’) -> str str(bytes_or_buffer[, encoding[, errors]]) -> str
Create a new string object from the given object. If encoding or errors is specified, then the object must expose a data buffer that will be decoded using the given encoding and error handler. Otherwise, returns the result of object.__str__() (if defined) or repr(object). encoding defaults to sys.getdefaultencoding(). errors defaults to ‘strict’.*
to_xml
to_xml (elm, lvl=0, indent:bool=True, do_escape:bool=True)
Convert ft
element tree into an XML string
= to_xml(samp, do_escape=False)
h print(h)
<!doctype html>
<html>
<head>
<title>Some page</title>
</head>
<body>
<div class="myclass another" style="padding:1; margin:2">
Some text
another line
<input name="jph's">
<img src="filename" data="1">
</div>
</body>
</html>
class PageTitle:
def __ft__(self): return H1("Hello")
class HomePage:
def __ft__(self): return Div(PageTitle(), Div('hello'))
= to_xml(Div(HomePage()))
h = """<div>
expected_output <div>
<h1>Hello</h1>
<div>hello</div>
</div>
</div>
"""
assert h == expected_output
= to_xml(samp, indent=False)
h print(h)
<!doctype html><html><head><title>Some page</title></head><body><div class="myclass another" style="padding:1; margin:2">Some text
another line<input name="jph's"><img src="filename" data="1"></div></body></html>
Interoperability both directions with Django and Jinja using the html() protocol:
def _esc(s): return s.__html__() if hasattr(s, '__html__') else Safe(escape(s))
= Safe('<b>Hello from Django</b>')
r print(to_xml(Div(r)))
print(_esc(Div(P('Hello from fastcore <3'))))
<div><b>Hello from Django</b></div>
<div>
<p>Hello from fastcore <3</p>
</div>
highlight
highlight (s, lang='html')
Markdown to syntax-highlight s
in language lang
FT.__call__
FT.__call__ (*c, **kw)
Call self as a function.
You can also reorder the children to come after the attrs, if you use this alternative syntax for FT
where the children are in a second pair of ()
(behind the scenes this is because FT
implements __call__
to add children).
='myclass')(
Body(klass='padding:3px')(
Div(style'Some text 1<2',
=True)('in italics'),
I(spurious='me'),
Input(name="filename", data=1)
Img(src
) )
<body class="myclass">
<div style="padding:3px">
<2
Some text 1<i spurious>in italics</i>
<input name="me">
<img src="filename" data="1">
</div>
</body>
getattr
__getattr__ (tag)