Package: whitehouse
WhitehouseWelcome to the Whitehouse HTML Components library. 8-) 😏Installation:pip install whitehouseThe whitehouse library:There are only four important files (i.e., modules) in this library (seriously... that's it... that's all there is to it...):base.pyThere is not much to this library. TheComponentclass inbase.pyis the base class that is responsible for constructing the tag for the element. TheComponentclass simply takes two argumentschildren(a list of child components) andattributes(a dictionary of HTML attributes to put on the element). TheComponentsimply recursively calls thestr()function to render all of its child components and then places the attributes into the element.default.pyA library of all of the basic HTML5 elements codified as components.utils.pyA library that provides functionalities such as formatting the rendered HTML into a more aethetically pleasing format.template.pyDefines aTemplateclass that you can use to create component templates. Use theTemplateclass when you need to create a template that includes thehtmlcomponent.Example Usage:This is how you can use whitehouse to create your own HTML components:fromwhitehouse.defaultimport*fromwhitehouse.customimportCustomComponentfromwhitehouse.utilsimportformat_htmlclassMyComponent(CustomComponent):def__init__(self,child1:Union[str,'Component',List['Component']],child2:Union[str,'Component',List['Component']],properties:Dict[str,str]={})->None:super().__init__([div([p("Hello, World 1!"),child1],{"class":"container"}),div([p("Hello, World 2!"),child2],{"class":"container"})],properties=properties)if__name__=="__main__":component=html([head([title("Hello, World!"),meta({"charset":"UTF-8","name":"viewport","content":"width=device-width, initial-scale=1.0"}),link({"rel":"stylesheet","href":"style.css"}),script("",{"src":"script.js"})]),body([MyComponent(p("Hello, World 3!"),p("Hello, World 4!"),{"id":"custom-component1"}),MyComponent(p("Hello, World 5!"),p("Hello, World 6!"),{"id":"custom-component2"}),script("console.log('Hello, World!');")])])print(format_html(component))Output HTML:<!DOCTYPE html><html><head><title>Hello, World!</title><metacharset="utf-8"content="width=device-width, initial-scale=1.0"name="viewport"/><linkhref="style.css"rel="stylesheet"/><scriptsrc="script.js"></script></head><body><mycomponentid="custom-component1"><divclass="container"><p>Hello, World 1!</p><p>Hello, World 3!</p></div><divclass="container"><p>Hello, World 2!</p><p>Hello, World 4!</p></div></mycomponent><mycomponentid="custom-component2"><divclass="container"><p>Hello, World 1!</p><p>Hello, World 5!</p></div><divclass="container"><p>Hello, World 2!</p><p>Hello, World 6!</p></div></mycomponent><script>console.log('Hello, World!');</script></body></html>Example usage #2:fragments=format_html([MyComponent(p("Hello, World 3!"),p("Hello, World 4!"),{"id":"custom-component1"}),MyComponent(p("Hello, World 5!"),p("Hello, World 6!"),{"id":"custom-component2"})])print(fragments)Output:<mycomponentid="custom-component1"><divclass="container"><p>Hello, World 1!</p><p>Hello, World 3!</p></div><divclass="container"><p>Hello, World 2!</p><p>Hello, World 4!</p></div></mycomponent><mycomponentid="custom-component2"><divclass="container"><p>Hello, World 1!</p><p>Hello, World 5!</p></div><divclass="container"><p>Hello, World 2!</p><p>Hello, World 6!</p></div></mycomponent>As you can see, no need for Jinja2 or any templating library necessary.Advantages of using whitehouse:Extremely lightweight:whitehousehas only 1 dependency, BeautifulSoup4, that is it.Easy to understand: it is fairly easy to see what HTML code will be generated from whitehouse Components. Obviously it is not as easy to see the HTML code that will be created by custom components so as a best practice, keep your custom components simple. In short, create and use custom components at your own peril.No need to create template files: all rendering is done in python.Create documentation: you can write PEP8 style documentation and type hints to describe the parameters and tell users how to use your custom components/templates.More compatible with code editors: by creating documentation for your components, your users will be able to see the documentation for you custom components/templates through their code editors.Ease of distribution: package your components/templates and its corresponding js and css files into your python package and then share it with other developers.Encapsulation: in the case of using Jinja2 and a web framework (e.g., FastAPI), if your template is inheriting another template, you may have to provide not just the data for the current template, but also its parent template. Withwhitehousecomponents, you can create a custom component that provides the parent component with the data it needs once and then you can reuse the custom component and provide only the necessary data for the custom component.Disadvantages of using whitehouse:Whitehouse currently doesn't support all HTML elements (seedefault.pyfor a list of all available HTML elements). This is mostly just because I'm too lazy to add support for every single HTML element out there. With that said, creating an element is super easy (again, see how to do it indefault.py) so feel free to create a branch and add your own elements in there, I promise I won't be too lazy to merge it into the main branch.Jinja2 vs whitehouse Comparison:For loops:{% for record in records %}<divid="record.id">{{ record.content }}</div>{% endfor }format_html([div(record.content,{"id":record.id})forrecordinrecords])Conditionals:{% if is_open == True %}<buttonclass=".open_btn">open</button>{% else %}<buttonclass=".close_btn">close</button>{% endif %}format_html(button("open",{"class":".open_btn"})ifis_openisTrueelsebutton("close",{"class":".close_btn"}))Template inheritance:Parent template:<!DOCTYPE html><html><head><title>Hello, World!</title><metacharset="utf-8"content="width=device-width, initial-scale=1.0"name="viewport"/></head><body>{% block body %}{% endblock %}</body></html>Child template:{% extends "base.html" %}

{% block body %}<mycomponentid="custom-component1"><divclass="container"><p>Hello, World 1!</p><p>Hello, World 3!</p></div><divclass="container"><p>Hello, World 2!</p><p>Hello, World 4!</p></div></mycomponent>{% endblock %}Output:<!DOCTYPE html><html><head><title>Hello, World!</title><metacharset="utf-8"content="width=device-width, initial-scale=1.0"name="viewport"/></head><body><mycomponentid="custom-component1"><divclass="container"><p>Hello, World 1!</p><p>Hello, World 3!</p></div><divclass="container"><p>Hello, World 2!</p><p>Hello, World 4!</p></div></mycomponent></body></html>template class:fromwhitehouse.customimportTemplateclassIndexTemplate(Template):def__init__(self,body_content:Component)->None:super().__init__(html([head([title("Hello, World!"),meta({"charset":"UTF-8","name":"viewport","content":"width=device-width, initial-scale=1.0"}),]),body([body_content])]))index_template=IndexTemplate(MyComponent(p("Hello, World 3!"),p("Hello, World 4!"),{"id":"custom-component1"}))print(format_html(index_template))Output:<!DOCTYPE html><html><head><title>Hello, World!</title><metacharset="utf-8"content="width=device-width, initial-scale=1.0"name="viewport"/></head><body><mycomponentid="custom-component1"><divclass="container"><p>Hello, World 1!</p><p>Hello, World 3!</p></div><divclass="container"><p>Hello, World 2!</p><p>Hello, World 4!</p></div></mycomponent></body></html>Why I built this library:I created this library because I needed a way of distributing HTML templates across a number of users of the Anacostia Pipeline.What I used to do was I packaged the Jinja2 templates into the Anacostia Pipeline package aspackage_dataand then publish the package onto PyPI. From there, users of Anacostia would have to use thepkg_resourceslibrary to extract the templates from the package and then create their own child templates. The architecture and development philosophy of Anactostia emphasized enabling developers to build their own Anacostia nodesandthe custom frontends to provide visualization for their nodes (i.e., I wanted developers to be able to create custom dashboards for their nodes). Enabling this development philosophy would require a server-side rendering approach where developers would create a FastAPI sub-application that would interact with their node(s), render the information of the nodes in an html fragment, and then the html fragment would be inserted into the DOM viahtmx. This approach was promising but since I was usingFastAPI, this approach presented a problem: only one directory can be defined for the main application, thus, users simply could not just create their own templates directory and then mount their templates directory into their sub-application which would then be recognized by the main application. Therefore, a more user-friendly approach was needed.The solution I decided upon was to create a library to render HTML fragments using only python which would not require the use of Jinja2 nor any templating directories that needed to be mounted into the FastAPI sub-application. Distribution of the fragments could be done by packaging the code for the fragments inside the Anacostia Pipeline package where the user could simply import the fragments into their python development environment. From there, the FastAPI sub-applications could simply render the templates during runtime and return the html snippets as the response to htmx requests.