Text Widgets#

class CodeWidget(code: str | None = None, language: str | None = None, line_numbers: bool = False, **kwargs: Any)[source]#

Bases: Block

A widget that displays code with syntax highlighting.

This widget uses Pygments to apply syntax highlighting to code snippets. The programming language must be specified to determine the highlighting rules.

Example

from wildewidgets import CodeWidget

python_code = '''
def hello_world():
    print("Hello, world!")
'''

code_widget = CodeWidget(
    code=python_code,
    language="python",
    line_numbers=True
)

Note

Requires the pygments package to be installed.

Parameters:
  • code – The code to be displayed

  • language – The programming language for syntax highlighting

  • line_numbers – Whether to display line numbers

  • **kwargs – Additional arguments passed to the parent class

Raises:
add_code(code: str, language: str, line_numbers: bool = False) None[source]#

Apply syntax highlighting to code and add it to the widget.

Parameters:
  • code – The code to highlight

  • language – The programming language to use for highlighting

  • line_numbers – Whether to display line numbers

block: str = 'wildewidgets_highlight_container'#

The name of the block for CSS styling

code: str = ''#

The code to be displayed

language: str | None = None#

The programming language for the code

line_numbers: bool = False#

If True, show line numbers

class HTMLWidget(*args: Any, **kwargs: Any)[source]#

Bases: TemplateWidget

A widget that renders raw HTML content.

This widget allows for displaying arbitrary HTML content within your Django application. Use with caution as it can introduce security risks if displaying user-provided content.

template_name#

The Django template used to render the widget

Type:

str | None

html#

The raw HTML content to display

Type:

str

css_class#

CSS classes to apply to the container

Type:

str | None

Example

from wildewidgets import HTMLWidget

html_content = '<p>This is <strong>HTML</strong> content</p>'
widget = HTMLWidget(html=html_content, css_class="custom-html-block")

Warning

Be careful when using this widget with user-provided content as it could lead to cross-site scripting (XSS) vulnerabilities.

Parameters:

*args – Positional arguments passed to the parent class

Keyword Arguments:

**kwargs – Keyword arguments including: html: The raw HTML content to display css_class: CSS classes to apply to the container

get_context_data(*args: Any, **kwargs: Any) dict[str, Any][source]#

Prepare the context data for template rendering.

Parameters:
  • *args – Positional arguments passed to the parent method

  • **kwargs – Keyword arguments passed to the parent method

Returns:

The context dictionary with HTML content and CSS classes

Return type:

dict

class MarkdownWidget(*args: Any, **kwargs: Any)[source]#

Bases: TemplateWidget

A widget that renders Markdown text as HTML.

This widget converts Markdown formatted text to HTML and displays it. It requires Django’s template system to render the HTML.

template_name#

The Django template used to render the widget

Type:

str | None

text#

The Markdown text to render

Type:

str

css_class#

CSS classes to apply to the container

Type:

str

Example

from wildewidgets import MarkdownWidget

markdown_text = '''
# Hello World

This is a **bold** statement with *emphasis*.

- Item 1
- Item 2
'''

widget = MarkdownWidget(text=markdown_text, css_class="my-markdown")
Parameters:

*args – Positional arguments passed to the parent class

Keyword Arguments:

**kwargs – Keyword arguments including: text: The Markdown text to render css_class: CSS classes to apply to the container

get_context_data(*args: Any, **kwargs: Any) dict[str, Any][source]#

Prepare the context data for template rendering.

Parameters:
  • *args – Positional arguments passed to the parent method

  • **kwargs – Keyword arguments passed to the parent method

Returns:

The context dictionary with markdown text and CSS classes

Return type:

dict

class StringBlock(text: str, **kwargs: Any)[source]#

Bases: Block

A basic widget that displays a string.

This is a simple wrapper around Block that initializes with a text string.

Deprecated since version 0.14.0: Use wildewidgets.Block directly instead. It works exactly like StringBlock

Parameters:
  • text – The text to display

  • **kwargs – Additional arguments passed to the parent class

Example

# Deprecated usage
from wildewidgets import StringBlock

text_block = StringBlock("Hello, world!")

# Preferred usage
from wildewidgets import Block

text_block = Block("Hello, world!")
Parameters:

text – The text to display

Keyword Arguments:

**kwargs – Additional arguments passed to the parent class

class TagBlock(text: str, color: str = 'secondary', **kwargs: Any)[source]#

Bases: Block

A widget that displays text as a colored badge/tag.

This widget creates a Bootstrap badge with customizable color, useful for status indicators, categories, or other labeled items.

Example

from wildewidgets import TagBlock

# Creates a success (green) badge
status_tag = TagBlock("Active", color="success")

# Creates a danger (red) badge
error_tag = TagBlock("Error", color="danger")

# Creates a custom colored badge
custom_tag = TagBlock("Custom", color="info")

Note

The color parameter accepts standard Bootstrap color names like “primary”, “secondary”, “success”, “danger”, “warning”, “info”, etc.

Parameters:

text – The text to display in the badge

Keyword Arguments:
  • color – The Bootstrap color class to use (default: “secondary”)

  • **kwargs – Additional arguments passed to the parent class

block: str = 'badge'#

block is the official wildewidgets name of the block; it can’t be changed by constructor kwargs

class TimeStamp(*blocks: str | wildewidgets.widgets.base.Widget | wildewidgets.widgets.base.Block, tag: str | None = None, name: str | None = None, modifier: str | None = None, css_class: str | None = None, css_id: str | None = None, empty: bool | None = None, script: str | None = None, attributes: dict[str, str] | None = None, data_attributes: dict[str, str] | None = None, aria_attributes: dict[str, str] | None = None)[source]#

Bases: Block

A widget that displays text formatted as a timestamp.

This widget renders text in a small font with light styling, making it suitable for timestamps, captions, or other secondary text.

tag#

HTML tag to use for the timestamp (small)

Type:

str

css_class#

CSS class for styling the timestamp

Type:

str

Example

from wildewidgets import TimeStamp

timestamp = TimeStamp("Last updated: 2023-06-15 14:30")
css_class: str = 'fw-light'#

A string of CSS classes to apply to this block

tag: str = 'small'#

The name of the HTML element to use as our tag, e.g. div