0

Is it possible to replace basic javascript (ex. getElementById, addEventListener, checked, forEach ...) with py-script Python codes ?

I want to work with html without learning JS, even ChatGPT didn't help to generate python equivalent code.

ex:

<body>
<input class="checkbox-input" id="oranges" type="radio" />
<input class="checkbox-input" id="bananas" type="radio" />
<a href="#" id="clear-button">&nbspClear</a>
<script>
document.getElementById('clear- 
button').addEventListener('click', function () {
  ["oranges", "bananas"].forEach(function(id) {
    document.getElementById(id).checked = false;
  });
  return false;
})
</script>
</div>
</body>
fond bcn
  • 15
  • 3
  • try using python Flask or Django package to use html – user19419589 May 07 '23 at 09:08
  • @user19419589 — You can't achieve what this JS does using server-side code. – Quentin May 07 '23 at 09:20
  • 1
    Pyscript indeed implements several types of JavaScript functionality as Python code, see for example https://stackoverflow.com/questions/73682009/how-do-i-get-the-value-of-checked-radio-button-in-pyscript for an event listener. However, this is a quite literal re-implementation, i.e., when you are learning the Pyscript functionality you are essentially learning JavaScript. Besides that, JS itself of course provides much more extensive and better documented functionality. So it would be advisable to learn JS anyway. – Marijn May 07 '23 at 09:21

1 Answers1

1

It is possible to interact with DOM from pyscript - see this answer.

However, all documentation, tutorials, books and online courses are for vanilla JavaScript. Pyscript is still a niche. Nobody is going to help you. If you do not have strong background in web development, it is likely to be easier for you to learn JavaScript in the first place and follow the common guides.

Mikko Ohtamaa
  • 82,057
  • 50
  • 264
  • 435