Spaces:
Sleeping
Sleeping
File size: 3,350 Bytes
6786adb 07ae381 3360e1e 07ae381 4e8efd7 07ae381 3360e1e 07ae381 3360e1e 07ae381 6786adb 07ae381 6786adb 07ae381 3916b69 07ae381 3916b69 07ae381 9c855f4 07ae381 0e6cba1 07ae381 9c855f4 07ae381 9c855f4 07ae381 ef143a9 07ae381 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
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()
|