+ 4
GUI-Based Python code in Webpage
I have the following Python code: https://sololearn.com/compiler-playground/cI8u3i2b4z9V/?ref=app And I want to run it in a webpage. Do note that it uses GUI graphics. I tried and barely got Pyscript running, then looked into other ones, but the GUI graphics from turtle isn't supported in any of the JavaScript libraries for running Python which I found(Brython, Pyodide). Is it possible to implement the code in my webpage, and if it is, how so?
11 Réponses
+ 4
Afnan Irtesum Chowdhury
Yes, you can run your code in web compiler using skulpt.
Here is a example of how can you use turtle in your webpage:
https://sololearn.com/compiler-playground/W4gB8gfenNFH/?ref=app
Skulpt doesn't support all python standard library modules and unfortunately colorsys isn't available in Skulpt's supported built-ins.
To solve the issue, you can manually re-implement colorsys functions in Python.
+ 3
Gulshan Mahawar I modified the code, but the canvas stays pitch black(empty), no changes happen
EDIT: Got it to work, but can't change the color(all stars are red). Need some help...
https://sololearn.com/compiler-playground/WYhf79dzRRiP/?ref=app
+ 3
Using Brython seems to take quite a lot of time to load the page, but thanks, since it fixed the issue. The speed is not a problem, we're not supposed to increase the speed over 10 anyways.
+ 2
Yeah I just can't change the color no matter what, RGB value is not changing.
+ 2
Afnan Irtesum Chowdhury
I used another way to try turtle on web and here it is-
https://sololearn.com/compiler-playground/WfSbMtX0Ov1S/?ref=app
But the problem I am getting is that when I try to put a bigger value in speed() (more than 10.4) it behaves like speed(0) and go too fast.
Edit: I fixed the issue, don't know you like it:-
Just replace for inner loop with this
`
for i in range(460):
rgb = hsv_to_rgb(h, 1, 0.8)
h += 1/n
pencolor(rgb_to_hex(rgb))
left(145)
for j in range(5):
speed(0)
forward(199)
speed(1)
forward(1)
left(150)
`
In this way we can slowed down the super fast turtle
+ 2
Now is the best time to start learning html5 canvas
+ 2
You can try the flask framework for running gui based python
+ 2
Afnan Irtesum Chowdhury I modified the code a bit, hopefully this works for you:-
https://sololearn.com/compiler-playground/WSYWbSOsJ6Ml/?ref=app
+ 2
RuntimeTerror
I can understand your frustration.
Python in webpages feels like looking at someone using a hammer to drive down a screw.
(Why? Javascript is the screwdriver! Use that instead of your clunky Python hammer 😁, you think you're Thor, the God of Hammers ?! 🔨)
The various Python solutions wouldn't replace the js dominance.
Maybe people are just more comfortable with Python and are ok with the trade-offs. Not everything is about performance. Fun and challenge are worthy motivations. Also, Python have a crazy big ecosystem and an even crazier fanbase...
+ 1
Afnan Irtesum Chowdhury
Try this:
`print(h)
print(r,g,b)
`
I found that h is getting modified in the loop but r,g,b isn't changing. That's pretty weird.
0
بدك تفحص موقع حط هاض الكود import requests
def افحص_الموقع(url):
try:
response = requests.get(url, timeout=5)
if response.status_code == 200:
print("✅ الموقع شغال!")
else:
print(f"⚠️ كود الاستجابة: {response.status_code}")
except requests.exceptions.RequestException:
print("❌ الموقع غير شغال أو فيه مشكلة.")
افحص_الموقع("https://example.com")