-
Notifications
You must be signed in to change notification settings - Fork 6
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
Codegen function from a tag string to support ReactPy's virtual DOM #9
Comments
I should mention that I used FastAPI/Uvicorn to run the web app because I couldn't get the dev web server to work on my development build from the tag string branch in #1 |
As expected, implementing support for ViewDOM is straightforward. Here's the necessary diff - we just need to adapt for the minor calling convention difference between the
See https://gist.github.com/jimbaker/8ca34e920eb7245a0d1cc093fa8e91f0 for the full code. |
So this almost works as expected:
resulting in
The problem is that the interpolation for the property |
Neat! I suspect trouble when the html parser buffers data -- I think if I call |
Hah, these examples needs some proper development now. I too was surprised I could get away with what I did with One nice observation is that for DOM libraries like IDOM, it is possible to mix usage like |
No need for a branch. Just create a folder. |
The problem with how we use Whether we should have a solution that depends on an internal part of At some point, I will add proper tests. |
If this ends up in the language we should add a new API to htmllib to help us here. Until then we can depend on internals or clone htmllib and hack on it, whatever works. It's just a demo. If we can do it by depending on a few internals that's good enough. |
The implementation from the Gist seems like something I wrote back when I was thinking of taking an approach similar to, but not quite the same as, |
The example code for this issue is now tracked in https://github.com/jimbaker/tagstr/blob/main/examples/htmltag.py |
Another reference implementation I found using a PEG parser: https://github.com/michaeljones/packed |
One trick that occurred to me when we're feeding the text into some kind of parser: Sometimes that parser may not be set up to handle interpolations (we already ran into this with the stdlib html parser). We could replace each interpolation with a special marker that includes the index of the interpolation (e.g. |
Jim's example includes the special marker. But there's still the open question about changing the parser internals. Do you think that's still on the table? |
I've honestly lost track what exactly this issue discusses, but if we wanted to add an API to the stdlib html parser to help it serve as a working example for this type of application I don't see why not. It would require someone (not me) to design that API and implement it, and some core dev (also not me) to approve it. I'm not sure if it would be required to spec this out in the PEP; my hunch is not (the PEP should focus on specifying the feature itself and providing a motivation so people understand how useful it might be). Honestly the stdlib html module seems a bit outdated, and there might be better 3rd party options available; we could see if one of those is interested in prototyping API support for tag strings. |
Let's close this ticket. I'm working on examples. I think I'll go in the direction of another example using a non-stdlib-htmlparser. |
We can avoid overhead of parsing a tag string on each usage by alternatively code generating a corresponding function, then memoizing it. I have implemented a gist to show how this can be done. This code implements an
html
tag for ReactPy:HTMLParser
from the stdlib, in a similar fashion as done in I am working in a branch #1.compiled(vdom, /, *args)
, wherevdom
is the DOM constructing function. In this case, thevdom
function inidom.core.vdom
, and it is very similar to other tools like ViewDOM'sVDOMNode
constructor. I expect this will be a common pattern.For HTML without interpolation, this looks like the following. First, the example from the IDOM docs, followed by the equivalent with the new tag:
Next I tried plugging this into a Hello, World example from ReactPy's intro:
Then assuming the above is in
main.py
, run withuvicorn main:app
This requires installing reactpy, fastapi, and uvicorn. So there's some setup required, but interesting to see how tag strings work with an interesting project.
The text was updated successfully, but these errors were encountered: