Text Widgets#
- class CodeWidget(code: str | None = None, language: str | None = None, line_numbers: bool = False, **kwargs: Any)[source]#
Bases:
BlockA 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:
ValueError – If no language is specified
RuntimeError – If Pygments is not installed
- class HTMLWidget(*args: Any, **kwargs: Any)[source]#
Bases:
TemplateWidgetA 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.
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:
- class MarkdownWidget(*args: Any, **kwargs: Any)[source]#
Bases:
TemplateWidgetA 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.
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:
- class StringBlock(text: str, **kwargs: Any)[source]#
Bases:
BlockA 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.Blockdirectly instead. It works exactly likeStringBlock- 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:
BlockA 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
- 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:
BlockA 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.
Example
from wildewidgets import TimeStamp timestamp = TimeStamp("Last updated: 2023-06-15 14:30")