Skip to content

Commit

Permalink
Fix: Handle start_time changes in st.video (streamlit#7257)
Browse files Browse the repository at this point in the history
Adds additional check to useEffect to handle changes in start_time for st.video
  • Loading branch information
mayagbarnes authored and Your Name committed Mar 22, 2024
1 parent 1c4d736 commit 044c566
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 2 deletions.
4 changes: 4 additions & 0 deletions e2e/scripts/st_video.py
Expand Up @@ -19,3 +19,7 @@
url = "https://www.w3schools.com/html/mov_bbb.mp4"
file = requests.get(url).content
st.video(file)

# Test start time with widget
timestamp = st.number_input("Start Time (in seconds)", min_value=0, value=6)
st.video(url, start_time=int(timestamp))
17 changes: 16 additions & 1 deletion e2e/specs/st_video.spec.js
Expand Up @@ -22,6 +22,21 @@ describe("st.video", () => {
});

it("displays a video player", () => {
cy.get(".element-container .stVideo").should("have.attr", "src");
cy.getIndexed(".element-container .stVideo", 0).should("have.attr", "src");
});

it("handles a start time", () => {
cy.getIndexed(".element-container .stVideo", 1).should("have.attr", "src");
});

it("handles changes in start time", () => {
// Change the start time from 6 to 5
cy.get(".element-container .stNumberInput .step-down").click();

// Wait for the video start time to update
cy.wait(3000);

// Confirm video updated
cy.getIndexed(".element-container .stVideo", 1).matchImageSnapshot("video-updated-start");
});
});
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 8 additions & 1 deletion frontend/lib/src/components/elements/Video/Video.tsx
Expand Up @@ -35,7 +35,14 @@ export default function Video({

/* Element may contain "url" or "data" property. */

const { type, url } = element
const { type, url, startTime } = element

// Handle startTime changes
useEffect(() => {
if (videoRef.current) {
videoRef.current.currentTime = startTime
}
}, [startTime])

useEffect(() => {
const videoNode = videoRef.current
Expand Down

0 comments on commit 044c566

Please sign in to comment.