Client side Python and skulpt

I’m interested, if you’re able to share a clone either here or in Show and Tell.

More broadly, I have repeatedly had frustrations similar to what you describe here. Because the client-side is based on a partial Python implementation with no systematic documentation of which parts of Python work and which don’t, it’s a continual guessing game. My sense is that experienced Anvilistas already know from experience the contours of what works and what doesn’t, so this ambiguity doesn’t bother them too much. But for a relatively new Python programmer like myself, when I try to do something (beyond the tutorials and other documented code examples) and it doesn’t work, there’s no easy way to determine whether I’m doing the Python wrong or that part of Python just isn’t implemented client-side (or sometimes with regard to Data Tables or another feature). I go back and forth between the Python docs and the Anvil docs. I consider writing a forum post but I often don’t feel I understand what’s going on well enough to even ask the question–without spending a lot of time on it. Over time, I’ve developed a sense of paralysis around using new features of Python I learn about.

In a recent example, I was trying to set random.seed based on today’s date. (I wanted a new random choice each day, but I wanted it to be the same throughout the day.) Based on a misreading of the Python docs, I initially set the seed using str(date), thinking that random.seed() would automatically convert any string into a number using something like a hash. It wasn’t triggering any Exception but I quickly realized it wasn’t setting a different seed on different days. My first thought was that Anvil/Skulpt just hadn’t fully implemented random.seed(), so I tried to dig into the Skulpt code and figure out the issue that way. I couldn’t understand or even find the relevant Skulpt code, so I gave up on using a string to set the seed and instead used date.toordinal(). And in the process, I realized that I had been reading the Python docs wrong: you can only use a string in random.seed() if the string contains an integer, basically. But the ambiguity in the client-side Python implementation made that troubleshooting a difficult process, at least for this newbie.

(Update: Now I’m seeing that random.seed(“A”) does give a result different from random.seed(“ABCD”) in standard Python 3.9, though it doesn’t in client-side Anvil: Anvil | Login So I guess my initial sense was actually correct, that my original code would work if the client-side Python were implemented fully/correctly. By the way, I don’t want this to come across as a complaint, receiving all of this for free as I do, but to those who have the knowhow: improving Skulpt so that the client-side Python is more reliable would make a huge difference for me.)

I wouldn’t dig around the skulpt code too long. It’s almost entirely written in JavaScript and you won’t find particularly user friendly documentation there.

Definitely check on the forum first. Even if forming the question is difficult.

The aim of the client side code is to keep moving in the direction of python correctness so do flag things when they come up.

1 Like

So this forum is a good place to flag Skulpt issues? I’ve wondered whether I should instead try to post Issues on the Skulpt Github, but I feel less comfortable doing that at this point, especially without having an Anvil-independent Skulpt implementation set up to confirm that the problem is actually with Skulpt.

Yes flag client side issues in the anvil forum is best. A bug in skulpt or missing feature is a bug or a missing feature in anvil.

Also it will give other anvil users a reference when they come across the issue in the future.

We try to stay in line with skulpt open source project and contribute heavily towards it (you might spy :male_detective:‍♂ a recent pull request from me implementing an updated datetime module that will be in the pipeline to fix the issues you previously pointed out).

I’ve split this topic as we’ve taken a slight tangent to the original thread.

3 Likes

Going down some Skulpt rabbit holes just now, I have learned a few lessons:

  • It is an incredibly immense undertaking to implement Python.
  • I need to take a more pragmatic approach to coding and manage my time and attention better, accepting “good enough” code that works rather than obsessively seeking 100% clarity.
  • To that end, Trinket.io provides a Skulpt REPL with which one can quickly see what works and what doesn’t (when it’s not something Anvil-specific).
  • They also provide some rudimentary (and outdated) Skulpt docs, that could perhaps be built upon.
  • A more up-to-date way to determine with confidence what Skulpt implements and what it doesn’t is to look at the Skulpt tests here: https://github.com/skulpt/skulpt/tree/master/test/unit3 (and I deeply appreciate your diligence adding to the testing suite, e.g. test_datetime.py).
1 Like

Isn’t it just :slight_smile:

I don’t think Trinket has updated their skulpt implementation for a while. skulpt.org might be a better place to check a few lines of code as that’s usually up to date. But while anvil tries to stay inline with skulpt we may at times be ahead or behind the project or differ at the edges. So it’s not always like for like.

For example anvil has the {**my_dict} syntax available where this is in the queue in skulpt.

You can also open up a module in anvil and run the script as the startup form. Some print statements will probably determine what you want.

2 Likes

This gave me the idea for a feature request:

Give me a debugger with breakpoints. Would save me millions of print statements a week.

1 Like

You may make a FR to express this formally and attract votes. There may even be one already.