def test_st_image_PIL_array(self): """Test st.image with a PIL array.""" imgs = [ Image.new("RGB", (64, 64), color="red"), Image.new("RGB", (64, 64), color="blue"), Image.new("RGB", (64, 64), color="green"), ] st.image( imgs, caption=["some caption"] * 3, width=200, use_column_width=True, clamp=True, format="PNG", ) el = self.get_delta_from_queue().new_element self.assertEqual(el.imgs.width, -2) # locate resultant file in the file manager and check its metadata. from streamlit.elements.image_proto import _PIL_to_bytes for idx in range(len(imgs)): file_id = _get_file_id(_PIL_to_bytes(imgs[idx], format="PNG"), "image/png") self.assertEqual(el.imgs.imgs[idx].caption, "some caption") self.assertTrue(file_id in media_file_manager) afile = media_file_manager.get(file_id) self.assertEqual(afile.mimetype, "image/png") self.assertEqual(afile.url, el.imgs.imgs[idx].url)
def test_st_audio(self): """Test st.audio.""" # Fake audio data: expect the resultant mimetype to be audio default. fake_audio_data = "\x11\x22\x33\x44\x55\x66".encode("utf-8") st.audio(fake_audio_data) el = self.get_delta_from_queue().new_element # locate resultant file in MediaFileManager and test its properties. file_id = _get_file_id(fake_audio_data, "audio/wav") self.assertTrue(file_id in media_file_manager) afile = media_file_manager.get(file_id) self.assertEqual(afile.mimetype, "audio/wav") self.assertEqual(afile.url, el.audio.url) # Test using generated data in a file-like object. sampleRate = 44100 frequency = 440 length = 5 t = np.linspace(0, length, sampleRate * length) # Produces a 5 second Audio-File y = np.sin(frequency * 2 * np.pi * t) # Has frequency of 440Hz wavfile.write("test.wav", sampleRate, y) with io.open("test.wav", "rb") as f: st.audio(f) el = self.get_delta_from_queue().new_element self.assertTrue(".wav" in el.audio.url) os.remove("test.wav") # Test using a URL instead of data some_url = "https://www.soundhelix.com/examples/mp3/SoundHelix-Song-3.mp3" st.audio(some_url) el = self.get_delta_from_queue().new_element self.assertEqual(el.audio.url, some_url) # Test that a non-URL string is assumed to be a filename bad_filename = "blah" with self.assertRaises(FileNotFoundError): st.audio(bad_filename) # Test that we can use an empty/None value without error. st.audio(None) el = self.get_delta_from_queue().new_element self.assertEqual(el.audio.url, "") # Test that our other data types don't result in an error. st.audio(b"bytes_data") st.audio("str_data".encode("utf-8")) st.audio(BytesIO(b"bytesio_data")) st.audio(np.array([0, 1, 2, 3]))
def test_st_video(self): """Test st.video.""" # Make up some bytes to pretend we have a video. The server should not vet # the video before sending it to the browser. fake_video_data = "\x12\x10\x35\x44\x55\x66".encode("utf-8") st.video(fake_video_data) el = self.get_delta_from_queue().new_element # locate resultant file in MediaFileManager and test its properties. file_id = _get_file_id(fake_video_data, "video/mp4") self.assertTrue(file_id in media_file_manager) afile = media_file_manager.get(file_id) self.assertEqual(afile.mimetype, "video/mp4") self.assertEqual(afile.url, el.video.url) # Test with an arbitrary URL in place of data some_url = "http://www.marmosetcare.com/video/in-the-wild/intro.webm" st.video(some_url) el = self.get_delta_from_queue().new_element self.assertEqual(el.video.url, some_url) # Test with sufficiently varied youtube URLs yt_urls = ( "https://youtu.be/_T8LGqJtuGc", "https://www.youtube.com/watch?v=kmfC-i9WgH0", "https://www.youtube.com/embed/sSn4e1lLVpA", ) yt_embeds = ( "https://www.youtube.com/embed/_T8LGqJtuGc", "https://www.youtube.com/embed/kmfC-i9WgH0", "https://www.youtube.com/embed/sSn4e1lLVpA", ) # url should be transformed into an embed link (or left alone). for x in range(0, len(yt_urls)): st.video(yt_urls[x]) el = self.get_delta_from_queue().new_element self.assertEqual(el.video.url, yt_embeds[x]) # Test that a non-URL string is assumed to be a filename bad_filename = "blah" with self.assertRaises(FileNotFoundError): st.video(bad_filename) # Test that we can use an empty/None value without error. st.video(None) el = self.get_delta_from_queue().new_element self.assertEqual(el.video.url, "") # Test that our other data types don't result in an error. st.video(b"bytes_data") st.video("str_data".encode("utf-8")) st.video(BytesIO(b"bytesio_data")) st.video(np.array([0, 1, 2, 3]))
def test_st_image_PIL_image(self): """Test st.image with PIL image.""" img = Image.new("RGB", (64, 64), color="red") st.image(img, caption="some caption", width=100, format="PNG") el = self.get_delta_from_queue().new_element self.assertEqual(el.imgs.width, 100) self.assertEqual(el.imgs.imgs[0].caption, "some caption") # locate resultant file in the file manager and check its metadata. from streamlit.elements.image_proto import _PIL_to_bytes file_id = _get_file_id(_PIL_to_bytes(img, format="PNG"), "image/png") self.assertTrue(file_id in media_file_manager) afile = media_file_manager.get(file_id) self.assertEqual(afile.mimetype, "image/png") self.assertEqual(afile.url, el.imgs.imgs[0].url)
def get(self, filename): # Filename is {requested_hash}.{extension} but MediaFileManager # is indexed by requested_hash. requested_hash = filename.split(".")[0] LOGGER.debug("MediaFileHandler: GET %s" % filename) try: media = media_file_manager.get(requested_hash) except: LOGGER.error("MediaFileManager: Missing file %s" % requested_hash) self.write("%s not found" % requested_hash) self.set_status(404) return LOGGER.debug("MediaFileManager: Sending %s file %s" % (media.mimetype, requested_hash)) self.write(media.content) self.set_header("Content-Type", media.mimetype) self.set_status(200)