function mimebundle_to_html
Converts a MIME bundle (dictionary or tuple of data and metadata) into HTML string representation, including any embedded JavaScript.
/tf/active/vicechatdev/patches/util.py
2203 - 2215
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
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function markdown_to_html 54.2% similar
-
function basic_markdown_to_html 54.1% similar
-
function html_to_markdown_v1 53.1% similar
-
function html_to_markdown 52.9% similar
-
function simple_markdown_to_html 52.8% similar