awacke1's picture
Update app.py
e29354c verified
raw
history blame
1.66 kB
import streamlit as st
from datasets import load_dataset
import streamlit.components.v1 as components
import requests
# Load the dataset
dataset = load_dataset("awacke1/DatasetOfDatasetsUSA")
# Initialize session state for record navigation and autoplay
if 'index' not in st.session_state:
st.session_state.index = 0
if 'autoplay' not in st.session_state:
st.session_state.autoplay = False
# Define the maximum index as the length of the dataset - 1
max_index = len(dataset['train']) - 1
def advance_record():
if st.session_state.index < max_index:
st.session_state.index += 1
else:
st.session_state.autoplay = False # Stop when reaching the end of the dataset
# Autoplay control
col1, col2 = st.columns([1, 10])
with col1:
if st.button('▢️ Play'):
st.session_state.autoplay = not st.session_state.autoplay
if st.session_state.autoplay:
st.experimental_rerun()
# If autoplay is enabled, advance the record every second
if st.session_state.autoplay:
st.session_state.time = st.session_state.get('time', 0) + 1
st.write(f"Autoplaying... Record {st.session_state.index + 1} of {max_index + 1}")
st.experimental_rerun()
advance_record()
item = dataset['train'][st.session_state.index]
link = item['link']
# Fetch and display the HTML content
try:
response = requests.get(link)
if response.status_code == 200:
components.html(response.text, height=600, scrolling=True)
else:
st.error(f"Failed to load content from {link}")
except Exception as e:
st.error(f"An error occurred: {e}")
# Note: Adjust the delay and autoplay logic as needed