module documentation

Custom tags and filters for Django's HTML templating.

Surprisingly, Django's templating does not provide some basic functionality I expected to be there. So, I had to write some custom tags (like the sum of a list) and filters specific to LyProX's needs.

Class MyMathExtension Custom math extension for markdown.
Function barplot_css Return the relative width to be sent to the CSS for the dashboard's bar plot.
Function capitalize_subsite Capitalize the subsite name.
Function concat Concatenate this & other.
Function custom_markdown Render custom markdown with footnotes and tables.
Function get Get an item from dict-like mapping using key.
Function get_logo Get the logo of the institution providing a dataset.
Function get_percent Get the percentage value_counts[key] in the total of value_counts.
Function get_subsite Get the subsite name from the dataset name.
Function getattr_ Get an attribute from instance using attr.
Function include_md Include a markdown file in the template.
Function index Get the i-th element of seq.
Function multiply Multiply value by factor.
Function remove_host Remove host from url. So, https://foo.com/bar/baz becomes bar/baz.
Function render_json Format and render raw JSON text.
Function render_md Render raw markdown text.
Function render_yaml Format and render raw YAML text.
Function safe_eval Safely evaluate an expression.
Function sum_ Implement the sum im Django templates.
Function to_list Convert arguments to a list.
Type Variable KT Undocumented
Type Variable N Undocumented
Type Variable T Undocumented
Type Variable VT Undocumented
Variable logger Undocumented
Variable register Undocumented
@register.filter(name='barplot_css')
def barplot_css(value_counts: dict[Any, int], argstr: str) -> float:

Return the relative width to be sent to the CSS for the dashboard's bar plot.

Use it like this in the HTML templates: {{ value_counts|barplot_css:'<key>,<width>' }}, where the key and the total width are separated by a comma. The key is the key of the value in the value_counts dict and the width is the total width of the bar plot.

@register.filter(name='capitalize_subsite')
def capitalize_subsite(subsite: str) -> str:

Capitalize the subsite name.

@register.filter(name='concat')
def concat(this: str, other: str) -> str:

Concatenate this & other.

def custom_markdown(text: str) -> str:

Render custom markdown with footnotes and tables.

@register.filter(name='get')
def get(mapping: Mapping[KT, VT], key: KT) -> VT | None:

Get an item from dict-like mapping using key.

@register.filter(name='get_logo')
def get_logo(dataset: str) -> str:

Get the logo of the institution providing a dataset.

@register.filter(name='get_percent')
def get_percent(value_counts: dict[Any, int], key: Any) -> str:

Get the percentage value_counts[key] in the total of value_counts.

Use it like this in the HTML templates: {{ value_counts|get_percent:key }}.

@register.filter(name='get_subsite')
def get_subsite(dataset: str) -> str:

Get the subsite name from the dataset name.

@register.filter(name='getattr')
def getattr_(instance, attr) -> Any:

Get an attribute from instance using attr.

@register.simple_tag(name='include_md', takes_context=True)
def include_md(context: template.RequestContext, template_name: str) -> str:

Include a markdown file in the template.

The markdown file may also include Django template tags, just as the HTML. This tag is used like this: {% include_md "path/to/file.md" %}.

@register.filter(name='index')
def index(seq: Sequence[T], i: str) -> T:

Get the i-th element of seq.

Allows one to use {{ seq|index:'i' }} in Django templates.

@register.filter(name='multiply')
def multiply(value: N, factor: N) -> N:

Multiply value by factor.

@register.filter(name='remove_host')
def remove_host(url: str) -> str:

Remove host from url. So, https://foo.com/bar/baz becomes bar/baz.

@register.simple_tag(name='render_json')
def render_json(raw: str) -> str:

Format and render raw JSON text.

@register.simple_tag(name='render_md')
def render_md(raw: str):

Render raw markdown text.

@register.simple_tag(name='render_yaml')
def render_yaml(raw: str) -> str:

Format and render raw YAML text.

def safe_eval(expr: str) -> Any:

Safely evaluate an expression.

@register.filter(name='sum')
def sum_(iterable: Iterable[N]) -> N:

Implement the sum im Django templates.

Use it like this: {{ mylist|sum }} or {{ mydict.values|sum }}.

@register.simple_tag(name='to_list')
def to_list(*args) -> list:

Convert arguments to a list.

This is useful if you want to iterate over a list of items in a Django template and you don't want to provide that list to the context, but 'harcoded' in the template. Use it like this: {% to_list 1 2 3 4 as mylist %} and then afterwards you can iterate over mylist.

KT =

Undocumented

Value
TypeVar('KT')
N =

Undocumented

Value
TypeVar('N', int, float)
T =

Undocumented

Value
TypeVar('T')
VT =

Undocumented

Value
TypeVar('VT')
logger =

Undocumented

register =

Undocumented