pyglove icon indicating copy to clipboard operation
pyglove copied to clipboard

Add HTML conversion support.

Open copybara-service[bot] opened this issue 5 months ago • 0 comments

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[bot] avatar Sep 28 '24 07:09 copybara-service[bot]