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

HTML not getting rendered everytime. #370

Closed
kumargautam99 opened this issue May 14, 2022 · 19 comments · Fixed by #397
Closed

HTML not getting rendered everytime. #370

kumargautam99 opened this issue May 14, 2022 · 19 comments · Fixed by #397
Labels
type: bug Something isn't working

Comments

@kumargautam99
Copy link

kumargautam99 commented May 14, 2022

I tried the sample script for computing the value of pi.

Using the CDN url of pyscript.js and pyscript.css , 80% of the times the render is happening after Loading runtime...
But sometimes it gives the pink color section as output and no content.

Using the pyscript.js and pyscript.css locally, the Loading runtime.. persists for minutes without any output.
render_issue

@rayjolt
Copy link

rayjolt commented May 14, 2022

I am seeing this issue as well, with the following HTML document:

<html>
  <head>
    <title>PyScript Test</title>
    <meta charset="utf-8" />
    <link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" />
    <script defer src="https://pyscript.net/alpha/pyscript.js"></script>
  </head>
  <body>
    <py-script>
print("Testing PyScript")
    </py-script>
  </body>
</html>

Looking at the console, I see the following error:

Uncaught (in promise) PythonError: Traceback (most recent call last):
  File "<exec>", line 164, in write
  File "<exec>", line 116, in write
JsException: SyntaxError: Document.querySelector: '#-2cfe72dd-aed7-7bb3-50f0-d5a53fa5d0ab' is not a valid selector
    PythonError error_handling.gen.ts:309
    new_error pyodide.asm.js:14
    callPyObjectKwargs pyproxy.gen.ts:374
    callKwargs pyproxy.gen.ts:1167
    evaluate base.ts:115
error_handling.gen.ts:309:4

This error occurred 5 times out of 10 when I tested it.
Tested on Firefox 100.0, Ubuntu 20.04 LTS.

@marianoweber
Copy link
Contributor

This seems to be a problem with the CDN build.
With the build from the CDN, I can reproduce it. Locally built from the source, I can't.

@pauleveritt
Copy link

There's a discussion in #359

@yannickfunk
Copy link
Contributor

@pauleveritt I think the issue here is a different issue than #359

@yannickfunk
Copy link
Contributor

yannickfunk commented May 14, 2022

I think the problem here is that this.constructor.name breaks in minified code.

There is name property that already exists in regular functions class constructors. A known pitfall is that it will be mangled in minified code, so it is generally useless in browser JS, and its use can be considered an antipattern. name cannot be reassigned (in some browsers), so a separate property is needed to identify the class.

(https://stackoverflow.com/questions/46217853/get-constructor-name-of-object)

This could then break the id creation, because it relies on constructor.name

if (!this.id) this.id = this.constructor.name + '-' + guidGenerator();

One fix could be to use the html tag name to generate the id prefix:

checkId() {
    const idPrefix = this.tagName.split("-").map(e => {
        e = e.toLowerCase();
        return e.charAt(0).toUpperCase() + e.slice(1)
    }).join("");
    if (!this.id) this.id = idPrefix + '-' + guidGenerator();
}

This will work for minified code aswell

@rayjolt rayjolt mentioned this issue May 15, 2022
@Navaneeth-Sharma
Copy link

Hi,
I am facing similar issue.

I observe these points

  • The text are rendered sometimes when refreshed.
  • The "live" server behaves the same.

Hi @yannickfunk, thank you for providing the solution. I am new to Javascript, Can you please guide where to put this snippet?

I tried adding

 <script> 
    checkId() {
    const idPrefix = this.tagName.split("-").map(e => {
        e = e.toLowerCase();
        return e.charAt(0).toUpperCase() + e.slice(1)
    }).join("");
    if (!this.id) this.id = idPrefix + '-' + guidGenerator();
}
</script>

But didnt work.

Thank you

@rayjolt
Copy link

rayjolt commented May 15, 2022

@Navaneeth-Sharma

Hi yannickfunk, thank you for providing the solution. I am new to Javascript, Can you please guide where to put this snippet?

This needs to be fixed inside PyScript itself. You can't fix it inside HTML documents you create. The snippet Yannick provided is meant to be applied here in the PyScript code; please wait while the PyScript devs fix it.

@marianoweber
Copy link
Contributor

checkId() {
    const idPrefix = this.tagName.split("-").map(e => {
        e = e.toLowerCase();
        return e.charAt(0).toUpperCase() + e.slice(1)
    }).join("");
    if (!this.id) this.id = idPrefix + '-' + guidGenerator();
}

Hey,

just to clarify:
With this function, the Id of elements like py-script would be "#Pyscript-xyz" instead of "#PyScript-xyz", correct?
Does anyone know if that is relevant in any other parts of the project?

Thanks

@yannickfunk
Copy link
Contributor

yannickfunk commented May 15, 2022

checkId() {
    const idPrefix = this.tagName.split("-").map(e => {
        e = e.toLowerCase();
        return e.charAt(0).toUpperCase() + e.slice(1)
    }).join("");
    if (!this.id) this.id = idPrefix + '-' + guidGenerator();
}

Hey,

just to clarify: With this function, the Id of elements like py-script would be "#Pyscript-xyz" instead of "#PyScript-xyz", correct? Does anyone know if that is relevant in any other parts of the project?

Thanks

No, it will still be "#PyScript-xyz".

"PY-SCRIPT".split("-") // => ["PY", "SCRIPT"]
.map(e.toLowercase()) // => ["py", "script"]
.map(e.charAt(0).toUpperCase() + e.slice(1)) // => ["Py", "Script"]
.join("") // => "PyScript"

@marianoweber
Copy link
Contributor

checkId() {

const idPrefix = this.tagName.split("-").map(e => {
    e = e.toLowerCase();
    return e.charAt(0).toUpperCase() + e.slice(1)
}).join("");
if (!this.id) this.id = idPrefix + '-' + guidGenerator();

}

Hey,

just to clarify: With this function, the Id of elements like py-script would be "#Pyscript-xyz" instead of "#PyScript-xyz", correct? Does anyone know if that is relevant in any other parts of the project?

Thanks

No, it will still be "#PyScript-xyz".

"PY-SCRIPT".split("-") // => ["PY", "SCRIPT"]

.map(e.toLowercase()) // => ["py", "script"]

.map(e.charAt(0).toUpperCase() + e.slice(1)) // => ["Py", "Script"]

.join("") // => "PyScript"

Got it, somehow overlooked the first split... thanks for the clarification.

@yannickfunk
Copy link
Contributor

@marimeireles In case this is missed, could you give the issue title a [BUG] tag and add the bug label?

@fpliger
Copy link
Contributor

fpliger commented May 16, 2022

@kumargautam99 can you try now? #395 should have temporarily fixed it (although we need to investigate to actually come with a better fix)

@kumargautam99
Copy link
Author

kumargautam99 commented May 16, 2022 via email

@fpliger
Copy link
Contributor

fpliger commented May 16, 2022

Can you make sure you try on an incognito browser or can you flush your cache?

If it still persists, can you please send some code for us to reproduce?

@kumargautam99
Copy link
Author

Tried both (incognito browser and cache flush), the issue persists.

Below is the sample code which i am using inside google chrome.
`

<script defer src="https://pyscript.net/alpha/pyscript.js"></script> print("Let's compute π:") def compute_pi(n): pi = 2 for i in range(1,n): pi *= 4 * i ** 2 / (4 * i ** 2 - 1) return pi

pi = compute_pi(100000)
s = f"π is approximately {pi:.3f}"
print(s)

`

@fpliger
Copy link
Contributor

fpliger commented May 16, 2022

@kumargautam99 Not sure I understand. This, pointing to the cdn, works just fine. What am I missing?

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width,initial-scale=1" />

    <title>Reproducing #370</title>

    <link rel="icon" type="image/png" href="favicon.png" />
    <link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" />
    <script defer src="https://pyscript.net/alpha/pyscript.js"></script>
  </head>

  <body>
    <py-script>
print("Let's compute π:") 
def compute_pi(n): 
    pi = 2
    for i in range(1,n): 
        pi *= 4 * i ** 2 / (4 * i ** 2 - 1) 
        return pi
pi = compute_pi(100000)
s = f"π is approximately {pi:.3f}"
print(s)
    </py-script>
  </body>
</html>

Can you paste your full example?

@rayjolt
Copy link

rayjolt commented May 17, 2022

I tested again with the code I posted above, and I can confirm that the issue is fixed. Thank you for your work on this.

@fpliger
Copy link
Contributor

fpliger commented May 17, 2022

Awesome! Thank you @rayjolt !

@Navaneeth-Sharma
Copy link

Thank you @fpliger and community ! The issue is resolved!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants