parvalijaved commited on
Commit
07ae381
1 Parent(s): 0e6cba1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +77 -53
app.py CHANGED
@@ -1,66 +1,90 @@
1
- import streamlit as st
2
- import requests
3
  import json
 
4
 
5
- # URL to the JSON file
6
- JSON_URL = "https://cdn.statically.io/gh/gita/gita/main/data/verse.json"
 
 
7
 
8
- # Fetch the JSON data
9
- @st.cache_data
10
- def fetch_data():
11
- response = requests.get(JSON_URL)
12
- response.raise_for_status()
13
- return response.json()
 
 
 
 
 
14
 
15
- # Streamlit app
16
- st.title("Bhagavad Gita")
17
 
18
- # Fetch and display chapters and verses
19
- data = fetch_data()
 
20
 
21
- # Check the type of data and parse accordingly
22
- if isinstance(data, list):
23
- chapters = {int(chapter["chapter_number"]): chapter for chapter in data}
24
- else: # Assuming data is a dictionary
25
- chapters = {int(chapter_number): details for chapter_number, details in data.items()}
26
 
27
- # Select Chapter
28
- chapter_id = st.selectbox("Select Chapter", sorted(chapters.keys()), format_func=lambda x: f"Chapter {x}")
29
 
30
- # Display chapter details if available
31
- chapter_details = chapters.get(chapter_id)
32
- if chapter_details:
33
- chapter_summary = chapter_details.get('chapter_summary', 'No summary available')
34
- st.subheader(f"Chapter {chapter_id}")
35
- st.markdown(f"**Summary:**\n\n{chapter_summary}")
36
 
37
- # Extract verses for the selected chapter if available
38
- verses = chapter_details.get('verses')
39
- if isinstance(verses, list) and verses:
40
- # Display all verses in the selected chapter
41
- st.subheader("All Verses in this Chapter")
42
- for verse in verses:
43
- verse_number = verse['verse_number']
44
- verse_text = verse['text']
45
- verse_translation = verse['word_meanings']
46
 
47
- st.markdown(f"**Verse {verse_number}**")
48
- st.markdown(f"**Sanskrit:** {verse_text}")
49
- st.markdown(f"**Translation:** {verse_translation}")
50
- st.markdown("---")
51
 
52
- # Select Verse
53
- verse_number = st.selectbox("Select Verse", [verse['verse_number'] for verse in verses])
 
 
 
 
 
 
54
 
55
- # Display selected verse
56
- selected_verse = next((verse for verse in verses if verse['verse_number'] == verse_number), None)
57
- if selected_verse:
58
- st.markdown(f"**Verse {selected_verse['verse_number']}**")
59
- st.markdown(f"**Sanskrit:**\n\n{selected_verse['text']}")
60
- st.markdown(f"**Translation:**\n\n{selected_verse['word_meanings']}")
 
 
61
  else:
62
- st.error("Verse details not found.")
63
- else:
64
- st.error("No verses found for this chapter.")
65
- else:
66
- st.error("Chapter details not found.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import json
2
+ import streamlit as st
3
 
4
+ # Load the JSON data from the specified file path
5
+ file_path = '/workspace/verse.json'
6
+ with open(file_path, 'r', encoding='utf-8') as f:
7
+ data = json.load(f)
8
 
9
+ # Ensure data is structured correctly
10
+ if isinstance(data, list):
11
+ chapters = {}
12
+ for item in data:
13
+ chapter_id = item['chapter_id']
14
+ if chapter_id not in chapters:
15
+ chapters[chapter_id] = []
16
+ chapters[chapter_id].append(item)
17
+ else:
18
+ st.error("The JSON data is not in the expected format.")
19
+ st.stop()
20
 
21
+ # Get sorted list of chapter IDs
22
+ chapter_ids = sorted(chapters.keys())
23
 
24
+ # Sidebar for chapter and verse selection
25
+ st.sidebar.title("Gita Explorer")
26
+ chapter_id = st.sidebar.selectbox("Select Chapter", chapter_ids)
27
 
28
+ # Check if the chapter selection has changed
29
+ if 'chapter_id' not in st.session_state or st.session_state.chapter_id != chapter_id:
30
+ st.session_state.chapter_id = chapter_id
31
+ st.session_state.verse_id = 1
 
32
 
33
+ selected_verses = {verse['verse_number']: verse for verse in chapters[chapter_id]}
 
34
 
35
+ # Check if verses are available in the expected format
36
+ if not selected_verses:
37
+ st.error("No verses found for this chapter or verses are not in expected format.")
38
+ st.stop()
 
 
39
 
40
+ # Verse selection dropdown
41
+ verse_id = st.sidebar.selectbox(
42
+ "Select Verse",
43
+ sorted(selected_verses.keys()),
44
+ index=sorted(selected_verses.keys()).index(st.session_state.verse_id)
45
+ )
 
 
 
46
 
47
+ # Update session state if verse is selected from dropdown
48
+ st.session_state.verse_id = verse_id
 
 
49
 
50
+ # Display the selected verse
51
+ verse = selected_verses[verse_id]
52
+ st.header(f"Chapter {verse['chapter_number']} Verse {verse['verse_number']}")
53
+ st.subheader(verse['text'])
54
+ st.text("Transliteration:")
55
+ st.write(verse.get('transliteration', "No transliteration available."))
56
+ st.text("Translation:")
57
+ st.write(verse.get('word_meanings', "No translation available."))
58
 
59
+ # Buttons to navigate through verses
60
+ col1, col2 = st.columns(2)
61
+
62
+ with col1:
63
+ if st.button("Previous"):
64
+ prev_verse_id = verse_id - 1
65
+ if prev_verse_id in selected_verses:
66
+ st.session_state.verse_id = prev_verse_id
67
  else:
68
+ # Move to the last verse of the previous chapter
69
+ prev_chapter_index = chapter_ids.index(chapter_id) - 1
70
+ if prev_chapter_index >= 0:
71
+ prev_chapter_id = chapter_ids[prev_chapter_index]
72
+ st.session_state.chapter_id = prev_chapter_id
73
+ prev_chapter_verses = {verse['verse_number']: verse for verse in chapters[prev_chapter_id]}
74
+ st.session_state.verse_id = max(prev_chapter_verses.keys())
75
+ st.experimental_rerun()
76
+
77
+ with col2:
78
+ if st.button("Next"):
79
+ next_verse_id = verse_id + 1
80
+ if next_verse_id in selected_verses:
81
+ st.session_state.verse_id = next_verse_id
82
+ else:
83
+ # Move to the first verse of the next chapter
84
+ next_chapter_index = chapter_ids.index(chapter_id) + 1
85
+ if next_chapter_index < len(chapter_ids):
86
+ next_chapter_id = chapter_ids[next_chapter_index]
87
+ st.session_state.chapter_id = next_chapter_id
88
+ next_chapter_verses = {verse['verse_number']: verse for verse in chapters[next_chapter_id]}
89
+ st.session_state.verse_id = min(next_chapter_verses.keys())
90
+ st.experimental_rerun()