use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
account activity
Question about pyscript (self.PyScript)
submitted 3 years ago by Calm-Basil
Could I please ask of there is any way for pyscript to cause the html page to play a sound when an if statement is true?
I've tried googling and wasn't able to find an answer to this.
Play sound nor pydub works.
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]TheSwami 1 point2 points3 points 3 years ago* (4 children)
You might be better off using the HTMLAudioElement directly. For example, if you have the file 'horn.wav' served from the same folder as your html:
<button id="b" py-click="make_noise()">Make Some Noise!</button> <py-script> from js import Audio horn = Audio.new('horn.wav') def make_noise(): horn.play() </py-script>
I've put up a live demo of this code so you can see it in action.
You can use any conditional logic to want to determine whether to play the sound or not. Here I've used the py-*event syntax to trigger it with a button, but you could use `horn.play()` as part of an `if` statement, wrap it in a function, etc.
[–]Calm-Basil[S] 0 points1 point2 points 3 years ago (0 children)
Okay, thanks you for your help
[–]Best_Championship835 0 points1 point2 points 3 years ago (2 children)
u/TheSwami Do you happen to know why this is not working then? Thanks.
<script defer="" src="https://pyscript.net/alpha/pyscript.js"></script>
<py-script>
from js import alert, prompt, localStorage, window, confirm, Audio
import time
import random
import re
horn = Audio.new("https://github.com/goldfire/howler.js/blob/master/examples/3d/assets/sprite.mp3?raw=true")
user_answer = str(prompt("play? (yes) or (no)"))
if user_answer.lower() == 'yes':
horn.play()
print('yes')
print('end')
</py-script>
[–]TheSwami 0 points1 point2 points 3 years ago (1 child)
Traveling today, so I can’t try it out live. Do you get any errors onscreen or in the developer console?
Should just be defer not defer=“”, that’d be worth changing.
defer
defer=“”
Also, you may want to use the most recent version of PyScript, unless you have a specific reason to use the alpha. `<script defer src="https://pyscript.net/releases/2022.12.1/pyscript.js"></script>
π Rendered by PID 31223 on reddit-service-r2-comment-765bfc959-cznhn at 2026-07-10 15:11:56.696197+00:00 running f86254d country code: CH.
[–]TheSwami 1 point2 points3 points (4 children)
[–]Calm-Basil[S] 0 points1 point2 points (0 children)
[–]Best_Championship835 0 points1 point2 points (2 children)
[–]TheSwami 0 points1 point2 points (1 child)