Assume I have a folder with 4 video files (named like "movie1.mp4", "movie2.mp4" etc.)
I want to use ipywidgets to let the user control which video he desires to see. Like this:
import ipywidgets as wd
from IPython.display import HTML
def showvideo(i):
HTML("""<video width="100" height="100" controls><source src="movie{}.mp4" type="video/mp4"></video>""".format(i))
wd.interact(showvideo, video_number = [1, 2, 3, 4]);
This doesn't work. but simply calling
from IPython.display import HTML
HTML("""<video width="100" height="100" controls><source src="movie1.mp4" type="video/mp4"></video>""")
works
Is there some python magic that I need to call?
You aren't actually displaying the result. Jupyter automatically attempts to display the result of the last line of code in a cell, so that's why your HTML is displaying when doing it manually, but if you are calling it progrmamatically or your HTML isn't on the last line, you have to manually call display()
on your HTML.
Additionally, you aren't specifying the parameter correctly. The parameter name in the function has to match what you supply to interact
. Changing your code to this should work:
def showvideo(video_number):
display(HTML("""<video width="100" height="100" controls><source src="movie{}.mp4" type="video/mp4"></video>""".format(video_number)))
wd.interact(showvideo, video_number = [1, 2, 3, 4]);
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加