nteract / vdom

🎄 Virtual DOM for Python
https://github.com/nteract/vdom/blob/master/docs/mimetype-spec.md
BSD 3-Clause "New" or "Revised" License
221 stars 34 forks source link

serving outside of jupyter/nteract #96

Open Casyfill opened 5 years ago

Casyfill commented 5 years ago

is there any way right now to serve the vdom outside of the notebook? Can't find that in the docs

rmorshea commented 5 years ago

@Casyfill I wrote https://github.com/rmorshea/idom to do just this - let me know what you think.

note: there are some slight differences in the event spec that I hope to get merged here soon.

You can try it out here: Binder

Casyfill commented 5 years ago

Thanks! It looks pretty cool (btw the mybynder example does not work ,raising

AttributeError: 'dict' object has no attribute 'unmount'

on any interactive execution.

But I was looking for the exact use of vdom package - is it possible?

rmorshea commented 5 years ago

@casyfill That error is curious, I don’t seem to see that when I run it. Maybe a concurrency problem? Can you post an issue with the stack trace?

And unfortunately vdom is pretty basic and relies on the notebook for rendering. However I wrote idom in such a way that it would be compatible with vdom’s model specification (with the exception of event handlers, for now). As a result, you should be able to use vdom with idom, so long as you dont use event handlers from the vdom package (though I haven’t tested this recently).

EDIT: You are able to turn VDOM objects into static HTML via VDOM.to_html. While event handlers are part of the VDOM spec to_html ignores them.

rgbkrk commented 5 years ago

You can serialize vdom to html using to_html on a vdom node:

In [1]: from vdom import div, h1, p, a

In [2]: layout = div(
   ...:   h1("Example"),
   ...:   p("Hello ", a("world", href="http://example.com"), "!")
   ...: )

In [4]: layout.to_html()
Out[4]: '<div><h1>Example</h1><p>Hello <a href="http://example.com">world</a>!</p></div>'

I can't recall if that's only on master or if we've shipped with that.

rmorshea commented 5 years ago

@rgbkrk it's in the latest release.

@Casyfill it probably goes without saying, but to_html is static HTML so it'll serve your needs so long as you don't require event handlers.

If you do want event handlers you should be able to use vdom inside of idom:

@idom.element
async def MyComponent(self):
    return vdom.p("hello world")