🔍 Code Extractor

function mimebundle_to_html

Maturity: 40

Converts a MIME bundle (dictionary or tuple of data and metadata) into HTML string representation, including any embedded JavaScript.

File:
/tf/active/vicechatdev/patches/util.py
Lines:
2203 - 2215
Complexity:
simple

Purpose

This function processes MIME bundle data structures commonly used in Jupyter notebooks and rich display systems to extract HTML and JavaScript content and combine them into a single HTML string. It handles both tuple format (data, metadata) and dictionary format bundles, extracting 'text/html' and 'application/javascript' MIME types and combining them into executable HTML.

Source Code

def mimebundle_to_html(bundle):
    """
    Converts a MIME bundle into HTML.
    """
    if isinstance(bundle, tuple):
        data, metadata = bundle
    else:
        data = bundle
    html = data.get('text/html', '')
    if 'application/javascript' in data:
        js = data['application/javascript']
        html += '\n<script type="application/javascript">{js}</script>'.format(js=js)
    return html

Parameters

Name Type Default Kind
bundle - - positional_or_keyword

Parameter Details

bundle: A MIME bundle that can be either a tuple of (data, metadata) where data is a dictionary of MIME type keys to content values, or a dictionary directly containing MIME type keys. The data dictionary should contain keys like 'text/html' and/or 'application/javascript' with their corresponding content as string values.

Return Value

Returns a string containing HTML markup. If 'text/html' exists in the bundle, it's included first. If 'application/javascript' exists, it's appended as a <script> tag with type='application/javascript'. Returns an empty string if no HTML or JavaScript content is found.

Usage Example

# Example 1: Using dictionary format
bundle_dict = {
    'text/html': '<div>Hello World</div>',
    'application/javascript': 'console.log("Hello");'
}
html_output = mimebundle_to_html(bundle_dict)
print(html_output)
# Output: <div>Hello World</div>
# <script type="application/javascript">console.log("Hello");</script>

# Example 2: Using tuple format
data = {'text/html': '<p>Content</p>'}
metadata = {}
bundle_tuple = (data, metadata)
html_output = mimebundle_to_html(bundle_tuple)
print(html_output)
# Output: <p>Content</p>

# Example 3: Only JavaScript
bundle_js = {'application/javascript': 'alert("test");'}
html_output = mimebundle_to_html(bundle_js)
print(html_output)
# Output: 
# <script type="application/javascript">alert("test");</script>

Best Practices

  • Ensure the bundle parameter contains valid MIME type keys ('text/html', 'application/javascript') if you expect output
  • Be cautious when rendering the returned HTML as it may contain executable JavaScript code
  • The function does not sanitize or validate the HTML/JavaScript content, so ensure input is from trusted sources
  • If using tuple format, the metadata is extracted but not currently used in the conversion
  • The function returns an empty string if no recognized MIME types are present, so check for empty returns if needed

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function markdown_to_html 54.2% similar

    Converts Markdown formatted text to HTML using the python-markdown library with multiple extensions, falling back to basic conversion if the library is unavailable.

    From: /tf/active/vicechatdev/vice_ai/complex_app.py
  • function basic_markdown_to_html 54.1% similar

    Converts basic Markdown syntax to HTML without using external Markdown libraries, handling headers, lists, code blocks, and inline formatting.

    From: /tf/active/vicechatdev/vice_ai/complex_app.py
  • function html_to_markdown_v1 53.1% similar

    Converts HTML markup to Markdown syntax, handling headers, code blocks, text formatting, links, lists, and paragraphs with proper spacing.

    From: /tf/active/vicechatdev/vice_ai/new_app.py
  • function html_to_markdown 52.9% similar

    Converts HTML text back to Markdown format using regex-based pattern matching and replacement, handling headers, code blocks, formatting, links, lists, and HTML entities.

    From: /tf/active/vicechatdev/vice_ai/complex_app.py
  • function simple_markdown_to_html 52.8% similar

    Converts a subset of Markdown syntax to clean HTML, supporting headers, bold text, unordered lists, and paragraphs.

    From: /tf/active/vicechatdev/vice_ai/new_app.py
← Back to Browse