Using <script> tags to bootstrap pyscript #1326
Replies: 8 comments 17 replies
-
I know that this proposal is counter to some historic decisions but I feel it deserves a strong review by the whole team. Using |
Beta Was this translation helpful? Give feedback.
-
Thank you for re-opening this discussion @WebReflection! Before continuing, let me link to some of the previous discussions around these topics:
For
They are clearly related, but I think it's better to discuss them separately. I think that the choice of Before starting, I'd like to underline a a design principle which I have been following very strictly in my pyscript work and to which I don't want to renounce unless it's absolutely necessary:
I know it is a big and tough one because it's basically the opposite of what the browser does by default. Back
About <script>console.log("1");</script>
<script type="py">js.console.log("3? Really?")</script>
<script>console.log("2")</script> The reasons why I said +0.75 instead of +1 comes from points 1 and 2 (let's say they count -0.05) and most importantly 8-11 (let's say -0.20). Let's try to solve them:
|
Beta Was this translation helpful? Give feedback.
-
@antocuni thank you for all the pointers and the summarized answer. There are a lot of interesting points I'd like to address right away but I want to take the time I need to read previous discussions and reply properly, like you've done already. I have relatives visiting this WE though, so this will be done on Monday or late Sunday, just to provide an eta for my answer. I hope this is OK, and thanks again for your time and thoughts 👍 |
Beta Was this translation helpful? Give feedback.
-
@antocuni once again, thanks for the links and background, it was worth for me to go through those so I can answer about all discussed topics and I'll try to go in order. Before starting ...
There are three pillars on the Web as we know and consume it:
We're not really trying hard here, we're just abusing a visual layout space that if it breaks could, or could not, show something that simply breaks the rest of the page. If we want to try hard helping our users, it's by documentation and code examples they can always safely copy and paste. Using the wrong element to represent code data blocks doesn't help anyone that is not understanding what is doing or what's going on. Moreover:
Last, but not least, using a code data block that always preserves line numbers and position of the chars in it (no entities shenanigans) allows us to implement in the future even a debugging mode with relative ease, because nothing in
so why have we decided to fight against the browser internals, instead of using the only place the browser itself gives us as a safe place to write code data blocks? Here my personal principles are more like: "if there are multiple ways of doing the same thing but one is better, always prefer the better one" (actually a citation of Douglas Crockford, inventor of JSON). In this discussion it's not even about being better, it's about the correct way VS one way that's not meant to be and that already caused various bugs + it makes our parsing slower, it uses more CPU and RAM for no gain, it breaks layout with ease and it always leaks to CSS or query selectors on the page (3rd party scripts that do a query on the document and a Here we have two options:
About your
|
Beta Was this translation helpful? Give feedback.
-
Wrapping up arguments, this table's goal is to summarize what we need to explain to our users around the tag we ask them to use (please note that a few subjective points have not been included on purpose) :
I hope I haven't missed anything and beside the first two points, described well by MDN or other sites already, everything else is documentation we need to write and maintain, or issues filed to take care of, or fix, whenever that's even possible, make discussions with 3rd party libraries that should ignore our content by default, or users should find a way to avoid having their code destroyed, and so on. I know this table might feel a bit "leady" toward |
Beta Was this translation helpful? Give feedback.
-
last after-thoughts for today: it looks to me that all previous decisions or discussions have been made to accommodate Python users that would like to use Python on the Web, and somehow don't know too much about the Web, but none of the decisions around this project have addressed Web developers that would also like to use Python on the Web ... the thing is that the former kind of user probably wants the easiest entry point to get started, but few chars more don't look like a real bummer (imho), and the latter kind of user will always complain about the current status quo (and related bugs) like it's been done in all the issues already linked within this post. In short: wouldn't it be great if we could welcome both kind of users, those that don't know Web and also those that actually do know how the Web works and are totally interested in having Python available in there too? [1] [1] I see myself in this picture, and I really like it! |
Beta Was this translation helpful? Give feedback.
-
Quick update just to not forget about this discussion and keep some history around it ... @hoodmane got bitten by a test that was trying to represent SVG within a The solution was to avoid writing If edit on top of that, our tests could be way more easy to reason about, without commenting every time that the intent was to write SVG or some HTML but |
Beta Was this translation helpful? Give feedback.
-
This has landed #1396 We can re-open or re-visit or even drop the code as it landed in a completely non-breaking, non-changing mode, but to whoever was following this conversation, we have options now, it's up to us using these or not. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I've demoed a quick PoC which uses
<script>
tags to bootstrap pyscript as opposite of using non visual related custom elements to do so, such as<py-script>
and<py-config>
, which differently form<py-repl>
,<py-terminal>
or others, don't really belong to the page as these don't represent any UI at all but, because these are defined as custom elements, can't neither bootstrap earlier on a page (i.e. within the<head>
tag).After the demo "100 questions were raised" so I've thought about creating a discussion instead to also hear some feedback from the pyscript community.
current status
We parse as toml some
<py-config>
element on the page and we are able to run Python code within<py-script>
custom elements. Both elements require some special treatment in at least these areas:example
We can see the highlight in GitHub already believes that's just HTML and the browser would show this indeed:
py-script alternative
Non UI related element can break the parser in unexpected and undesired ways, while elements that are not sensible to CSS and that are meant to contain code (
<script>
) never get parsed internally as meaningful HTML by the browser (there's only one exception to this rule but that exception fails already in current<py-script>
logic too).On top of that, any
<script type="...">
that is not recognized by the browser will do pretty much nothing: it won't block, it won't execute, it won't even reach the network if ansrc
is present but the type is not standard.Accordingly, the previous example that requires extra unnecessary work for the browser, could be instead written as such:
note: see the highlight here knows that's just code, not HTML
This approach is also resistant to third party libraries that would otherwise involuntarily poison the content of generic elements on the page and not only because a script tag can be anywhere, including the head element, also because it would be super breaking/bad if third party changes the content of such script.
I hope overall benefits of this approach are clear by now, but I'd be happy to expand further if needed ... but let's see what are the options!
possible variants
<script type="pyscript">
works too, but it "steal" a potentialtype
from the standards, so that similarcustom-element
naming convention seems to be more appropriate<script type="text/x-python">
uses the correct python mime type, but it doesn't strictly hint pyscript about its nature<script is="py-script" type="text/x-python">
uses custom elements builtin extends and represents the correct mime time. This is ideal and very correct for its purpose but:<py-script>
<script type="py-script">
which seems to be (imho) the best compromisepy-config alternative
For the very same
<py-script>
reasons, the configuration as toml should not accidentally break the layout, be visible to users and it could be just in the<head>
of any page, where a manifest (PWA), some configuration (importmap) or anything meant to bootstrap libraries and logic usually fit better (semantics).possible variants
<script type="py-config">
that contains either JSON or toml to help bootstrapping pyscript on the page, and we should probably make this consistent with whatever we chose eventually for the py-script caseimportmap
, which would give us the following advantages:This latter idea is basically described as such:
Demo
This is more or less exactly what I've demoed as PoC so that you can get an idea of how everything works behind the scene.
The zip contains a
pyscript
folder where you canpython -m http.server
in it and see results on your browser of choice.pyscript.zip
Final thoughts
First of all I want to make it clear that nothing in this proposal is mutually exclusive, or meant to replace current documentation and functionality, so that I am not proposing to remove those custom elements, I am proposing to migrate over time to something technically and practically better. However, if anyone is in favor of doing this ASAP instead and drop those bootstrap components, I would not oppose, but I don't think it's necessary, because both ways can coexist for a while and we can let developers decide which way is better/preferred or more robust.
Beta Was this translation helpful? Give feedback.
All reactions