-
Notifications
You must be signed in to change notification settings - Fork 291
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2b24618
commit 71711c4
Showing
21 changed files
with
210 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import mesop as me | ||
|
||
|
||
@me.page(path="/html_demo") | ||
def app(): | ||
me.html( | ||
""" | ||
Custom HTML | ||
<a href="https://google.github.io/mesop/" target="_blank">mesop</a> | ||
""" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
## Overview | ||
|
||
The HTML component allows you to add custom HTML to your Mesop app. | ||
|
||
> Note: the HTML is [sanitized by Angular](https://angular.dev/best-practices/security#sanitization-example) for web security reasons so potentially unsafe code like JavaScript is removed. | ||
## Examples | ||
|
||
<iframe class="component-demo" src="https://mesop-y677hytkra-uc.a.run.app/html"></iframe> | ||
|
||
```python | ||
--8<-- "demo/html_demo.py" | ||
``` | ||
|
||
## API | ||
|
||
::: mesop.components.html.html.html |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
load("//mesop/components:defs.bzl", "mesop_component") | ||
|
||
package( | ||
default_visibility = ["//build_defs:mesop_internal"], | ||
) | ||
|
||
mesop_component( | ||
name = "html", | ||
ng_deps = [ | ||
"//mesop/web/src/safe_iframe", | ||
], | ||
) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
load("//build_defs:defaults.bzl", "py_library") | ||
|
||
package( | ||
default_visibility = ["//build_defs:mesop_examples"], | ||
) | ||
|
||
py_library( | ||
name = "e2e", | ||
srcs = glob(["*.py"]), | ||
deps = [ | ||
"//mesop", | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from . import html_app as html_app |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import mesop as me | ||
|
||
|
||
@me.page(path="/components/html/e2e/html_app") | ||
def app(): | ||
me.html( | ||
""" | ||
Custom HTML | ||
<a href="https://google.github.io/mesop/" target="_blank">mesoplink</a> | ||
""" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import {test, expect} from '@playwright/test'; | ||
|
||
test('test', async ({page}) => { | ||
await page.goto('/components/html/e2e/html_app'); | ||
// mesop is the HTML link so we're checking that it's rendered. | ||
expect(await page.getByText('Custom HTML').textContent()).toContain( | ||
'mesoplink', | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<div [innerHTML]="config().getHtml()" [style]="getStyle()"></div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
syntax = "proto2"; | ||
|
||
package mesop.components.html; | ||
|
||
message HtmlType { | ||
optional string html = 1; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import mesop.components.html.html_pb2 as html_pb | ||
from mesop.component_helpers import ( | ||
Border, | ||
BorderSide, | ||
Style, | ||
insert_component, | ||
register_native_component, | ||
) | ||
|
||
|
||
@register_native_component | ||
def html( | ||
html: str = "", | ||
*, | ||
style: Style | None = None, | ||
key: str | None = None, | ||
): | ||
""" | ||
This function renders custom HTML inside an iframe for web security isolation. | ||
Args: | ||
html: The HTML content to be rendered. | ||
style: The style to apply to the embed, such as width and height. | ||
key: The component [key](../guides/components.md#component-key). | ||
""" | ||
if style is None: | ||
style = Style() | ||
if style.border is None: | ||
style.border = Border.all( | ||
BorderSide( | ||
width=0, | ||
) | ||
) | ||
insert_component( | ||
key=key, | ||
type_name="html", | ||
proto=html_pb.HtmlType( | ||
html=html, | ||
), | ||
style=style, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import {Component, ElementRef, Input, ViewChild} from '@angular/core'; | ||
import { | ||
Key, | ||
Style, | ||
Type, | ||
} from 'mesop/mesop/protos/ui_jspb_proto_pb/mesop/protos/ui_pb'; | ||
import {HtmlType} from 'mesop/mesop/components/html/html_jspb_proto_pb/mesop/components/html/html_pb'; | ||
import {formatStyle} from '../../web/src/utils/styles'; | ||
|
||
@Component({ | ||
selector: 'mesop-html', | ||
templateUrl: 'html.ng.html', | ||
standalone: true, | ||
}) | ||
export class HtmlComponent { | ||
@Input({required: true}) type!: Type; | ||
@Input() key!: Key; | ||
@Input() style!: Style; | ||
@ViewChild('iframe', {read: ElementRef}) iframe!: ElementRef; | ||
private _config!: HtmlType; | ||
private srcDoc!: string; | ||
|
||
ngOnChanges() { | ||
this._config = HtmlType.deserializeBinary( | ||
this.type.getValue() as unknown as Uint8Array, | ||
); | ||
// const previousSrcDoc = this.srcDoc; | ||
// this.srcDoc = this._config.getHtml()!; | ||
|
||
// // Reload iframe if the URL has changed. | ||
// if ( | ||
// this.srcDoc !== previousSrcDoc && | ||
// this.iframe && | ||
// this.iframe.nativeElement | ||
// ) { | ||
// this.loadIframe(); | ||
// } | ||
} | ||
|
||
ngAfterViewInit() { | ||
// this.loadIframe(); | ||
} | ||
|
||
config(): HtmlType { | ||
return this._config; | ||
} | ||
|
||
getStyle(): string { | ||
return formatStyle(this.style); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters