Where am I going wrong??
<meta charset=“utf-8”>
Bread Recipe Calculator
<label for=“quantidade_de_paes”>How many loaves do you want to make?
<input type=“number” id=“quantidade_de_paes” name=“quantidade_de_paes”>
<button id=“convert” type=“submit” pys-onClick=“convert”>Calcular receita
def convert(*ags, **kws):
quantidade_de_paes = document.getElementById(‘quantidade_de_paes’).value*;*
quantidade = quantidade_de_paes
quantidade_ok = int(quantidade)
farinha = quantidade_ok * 38.2
recipiente = farinha * 1.65
agua = farinha * 0.6
sal = farinha * 0.02
fermento = farinha * 0.02
melhorador_de_farinha = farinha * 0.01
message = (f’Quantidade de pães: {quantidade_de_paes}, Farinha: {farinha}g, Recipiente: {recipiente}g, Água: {agua}g, Sal: {sal}g, Fermento: {fermento}g, Melhorador de farinha: {melhorador_de_farinha}g’)
pyscript.write(“result”, message)
Hi @quimica.engenharia , and welcome to the forum!
Here is a version of your code that works (or at least the button does - whether the math is right is up to you). Some fixes that were necessary:
- For
Element.write()
to work, there needs to be an existing HTML tag on the page with a matching id (“result”)
- Using
pys-onClick()
is no longer preferred - the new style is py-click
. Note that py-click takes a string of code, not just a Callable, so the attribute is py-click=convert()
.
- Possibly a result of the copy-paste, but your Python code needs to be contained within a
<py-script>
tag.
- There are some stray semicolons/asterisks in your code (which maybe were a result of copy/paste?)
<script defer src="https://pyscript.net/releases/2022.12.1/pyscript.js"></script>
<link rel="stylesheet" href="https://pyscript.net/releases/2022.12.1/pyscript.css">
Bread Recipe Calculator
<label for="quantidade_de_paes">How many loaves do you want to make?
<input type="number" id="quantidade_de_paes" name="quantidade_de_paes">
<button id="convert" type="submit" py-click="convert()">Calcular receita
<div id="result"></div>
<py-script>
from js import document
def convert(*ags, **kws):
quantidade_de_paes = document.getElementById("quantidade_de_paes").value
quantidade = quantidade_de_paes
quantidade_ok = int(quantidade)
farinha = quantidade_ok * 38.2
recipiente = farinha * 1.65
agua = farinha * 0.6
sal = farinha * 0.02
fermento = farinha * 0.02
melhorador_de_farinha = farinha * 0.01
message = (f"Quantidade de pães: {quantidade_de_paes}, Farinha: {farinha}g, Recipiente: {recipiente}g, Água: {agua}g, Sal: {sal}g, Fermento: {fermento}g, Melhorador de farinha: {melhorador_de_farinha}g")
pyscript.write("result", message)
</py-script>
1 Like
Thanks for the help, it worked, another question is how to comment the code inside the <py-script> </py-script>
tag. By coincidence I found your videos on youtube this week, I don’t understand the English language well but I watched it, thanks again. JeffGlass, hugs from Brazil
Hi, inside of the section, all the syntax is same as in python file, so the comments should be the same, which means you can use # for the line, or “”" something “”" for multiline comments.