Spaces:
Sleeping
Sleeping
import json | |
import streamlit as st | |
# Load the JSON data from the specified file path | |
file_path = 'verse.json' | |
with open(file_path, 'r', encoding='utf-8') as f: | |
data = json.load(f) | |
# Ensure data is structured correctly | |
if isinstance(data, list): | |
chapters = {} | |
for item in data: | |
chapter_id = item['chapter_id'] | |
if chapter_id not in chapters: | |
chapters[chapter_id] = [] | |
chapters[chapter_id].append(item) | |
else: | |
st.error("The JSON data is not in the expected format.") | |
st.stop() | |
# Get sorted list of chapter IDs | |
chapter_ids = sorted(chapters.keys()) | |
# Sidebar for chapter and verse selection | |
st.sidebar.title("Gita Explorer") | |
chapter_id = st.sidebar.selectbox("Select Chapter", chapter_ids) | |
# Check if the chapter selection has changed | |
if 'chapter_id' not in st.session_state or st.session_state.chapter_id != chapter_id: | |
st.session_state.chapter_id = chapter_id | |
st.session_state.verse_id = 1 | |
selected_verses = {verse['verse_number']: verse for verse in chapters[chapter_id]} | |
# Check if verses are available in the expected format | |
if not selected_verses: | |
st.error("No verses found for this chapter or verses are not in expected format.") | |
st.stop() | |
# Verse selection dropdown | |
verse_id = st.sidebar.selectbox( | |
"Select Verse", | |
sorted(selected_verses.keys()), | |
index=sorted(selected_verses.keys()).index(st.session_state.verse_id) | |
) | |
# Update session state if verse is selected from dropdown | |
st.session_state.verse_id = verse_id | |
# Display the selected verse | |
verse = selected_verses[verse_id] | |
st.header(f"Chapter {verse['chapter_number']} Verse {verse['verse_number']}") | |
st.subheader(verse['text']) | |
st.text("Transliteration:") | |
st.write(verse.get('transliteration', "No transliteration available.")) | |
st.text("Translation:") | |
st.write(verse.get('word_meanings', "No translation available.")) | |
# Buttons to navigate through verses | |
col1, col2 = st.columns(2) | |
with col1: | |
if st.button("Previous"): | |
prev_verse_id = verse_id - 1 | |
if prev_verse_id in selected_verses: | |
st.session_state.verse_id = prev_verse_id | |
else: | |
# Move to the last verse of the previous chapter | |
prev_chapter_index = chapter_ids.index(chapter_id) - 1 | |
if prev_chapter_index >= 0: | |
prev_chapter_id = chapter_ids[prev_chapter_index] | |
st.session_state.chapter_id = prev_chapter_id | |
prev_chapter_verses = {verse['verse_number']: verse for verse in chapters[prev_chapter_id]} | |
st.session_state.verse_id = max(prev_chapter_verses.keys()) | |
st.experimental_rerun() | |
with col2: | |
if st.button("Next"): | |
next_verse_id = verse_id + 1 | |
if next_verse_id in selected_verses: | |
st.session_state.verse_id = next_verse_id | |
else: | |
# Move to the first verse of the next chapter | |
next_chapter_index = chapter_ids.index(chapter_id) + 1 | |
if next_chapter_index < len(chapter_ids): | |
next_chapter_id = chapter_ids[next_chapter_index] | |
st.session_state.chapter_id = next_chapter_id | |
next_chapter_verses = {verse['verse_number']: verse for verse in chapters[next_chapter_id]} | |
st.session_state.verse_id = min(next_chapter_verses.keys()) | |
st.experimental_rerun() | |