Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add HTML conversion support. #303

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

copybara-service[bot]
Copy link

Add HTML conversion support.

This CL adds pg.object_utils.html_conversion sub-module, which provides a framework for converting Python objects into HTML views throug pg.to_html). Users could simply inherit pg.HtmlConvertible` to add HTML support, e.g.

class Foo(pg.HtmlConvertible):
  def _html_content(self, **kwargs) -> pg.Html:
    s = pg.Html()
    s.write('<span class="my_span">Foo</span>')
    s.add_style(
       """
       .my_span {color: red;}
       """
    )
    s.add_script(
      """
      def myFunc(p1, p2) {console.log(p1, p2);}
      """
    )
    return s

pg.Html will take care rendering of style/script, ensuring they appear once and just once even the returned HTML object is a part of a larger HTML.

Btw, all pg.Symbolic classes implements pg.HtmlConvertible by default, so they could be easily inspected in Colab notebooks using pg.to_html(pg_object).

@copybara-service copybara-service bot force-pushed the test_679770145 branch 4 times, most recently from 94365e5 to 0a4d783 Compare October 2, 2024 07:24
This CL adds `pg.object_utils.html_conversion` sub-module, which provides a framework for converting Python objects into HTML views throug `pg.to_html). Users could simply inherit `pg.HtmlConvertible` to add HTML support, e.g.

```
class Foo(pg.HtmlConvertible):
  def _html_content(self, **kwargs) -> pg.Html:
    s = pg.Html()
    s.write('<span class="my_span">Foo</span>')
    s.add_style(
       """
       .my_span {color: red;}
       """
    )
    s.add_script(
      """
      def myFunc(p1, p2) {console.log(p1, p2);}
      """
    )
    return s
```

`pg.Html` will take care rendering of style/script, ensuring they appear once and just once even the returned HTML object is a part of a larger HTML.

Btw, all `pg.Symbolic` classes implements `pg.HtmlConvertible` by default, so they could be easily inspected in Colab notebooks using `pg.to_html(pg_object)`.

PiperOrigin-RevId: 679770145
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant