Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import requests | |
| import json | |
| import os | |
| BOOKMARKS_FILE = "bookmarks.json" | |
| st.set_page_config(page_title="๐ Quran App", layout="wide", page_icon="๐") | |
| # Use exact Bismillah version from API (with BOM character) | |
| BISMILLAH_TEXT = "๏ปฟุจูุณูู ู ุงูููููู ุงูุฑููุญูู ููฐูู ุงูุฑููุญููู ู" | |
| # Bookmark functions | |
| def load_bookmarks(): | |
| if os.path.exists(BOOKMARKS_FILE): | |
| with open(BOOKMARKS_FILE, "r") as f: | |
| return json.load(f) | |
| return [] | |
| def save_bookmarks_file(bookmarks): | |
| with open(BOOKMARKS_FILE, "w") as f: | |
| json.dump(bookmarks, f, indent=2) | |
| def add_bookmark(bm): | |
| bookmarks = load_bookmarks() | |
| if not any(b == bm for b in bookmarks): | |
| bookmarks.append(bm) | |
| save_bookmarks_file(bookmarks) | |
| st.sidebar.success(f"Bookmarked Ayah {bm['ayah']} from {bm['surah']}!") | |
| # API fetching functions | |
| def get_surah_list(): | |
| return requests.get("https://api.alquran.cloud/v1/surah").json()["data"] | |
| def fetch_data(url): | |
| return requests.get(url).json().get("data") | |
| def get_translation(surah, ayah=None, lang="en.asad"): | |
| url = f"https://api.alquran.cloud/v1/{'ayah' if ayah else 'surah'}/{surah}{(':'+str(ayah)) if ayah else ''}/{lang}" | |
| return fetch_data(url) | |
| def get_arabic_audio(surah, ayah=None): | |
| url = f"https://api.alquran.cloud/v1/{'ayah' if ayah else 'surah'}/{surah}{(':'+str(ayah)) if ayah else ''}/ar.alafasy" | |
| return fetch_data(url) | |
| # UI setup | |
| st.sidebar.header("Quran Explorer") | |
| lang = st.sidebar.selectbox("Translation Language", ["Arabic", "English", "Urdu", "French"]) | |
| lang_codes = {"Arabic": "ar", "English": "en.asad", "Urdu": "ur.junagarhi", "French": "fr.hamidullah"} | |
| lang_code = lang_codes[lang] | |
| surahs = get_surah_list() | |
| surah_names = [f"{s['number']}. {s['englishName']} ({s['name']})" for s in surahs] | |
| surah_sel = st.sidebar.selectbox("Select Surah", ["-- Select Surah --"] + surah_names) | |
| mode = st.sidebar.radio("Choose Mode", ("Full Surah", "Specific Ayah")) | |
| if surah_sel != "-- Select Surah --": | |
| surah_num = int(surah_sel.split(".")[0]) | |
| if mode == "Specific Ayah": | |
| ayah_num = st.sidebar.number_input("Ayah Number", min_value=1, step=1) | |
| st.markdown(""" | |
| <style> | |
| .highlight { | |
| background-color: #f3f3f3; | |
| padding: 1em; | |
| border-left: 5px solid #4b2e83; | |
| border-radius: 8px; | |
| margin: 1em 0; | |
| font-size: 1.1em; | |
| } | |
| </style> | |
| """, unsafe_allow_html=True) | |
| st.markdown("<h1 style='text-align:center;'>๐ Quran Explorer</h1>", unsafe_allow_html=True) | |
| st.markdown("---") | |
| bookmarks = load_bookmarks() | |
| if surah_sel != "-- Select Surah --": | |
| if mode == "Full Surah": | |
| trans = get_translation(surah_num, lang=lang_code) | |
| arabic = get_arabic_audio(surah_num) | |
| st.markdown(f"## {trans['englishName']} โ {trans['name']}") | |
| # Show Bismillah visually for full Surahs (except Surah 9) | |
| if surah_num != 9: | |
| st.markdown(f"<div class='highlight'><b>{BISMILLAH_TEXT}</b></div>", unsafe_allow_html=True) | |
| ayahs = list(zip(trans["ayahs"], arabic["ayahs"])) | |
| # Skip Bismillah if it's the only content in first ayah | |
| if ayahs and ayahs[0][0]["text"].strip() == BISMILLAH_TEXT and ayahs[0][0]["numberInSurah"] == 1: | |
| ayahs = ayahs[1:] | |
| for t, a in ayahs: | |
| ayah_number = t["numberInSurah"] | |
| with st.expander(f"Ayah {ayah_number}"): | |
| st.markdown(f"<div class='highlight'><b>{t['text']}</b></div>", unsafe_allow_html=True) | |
| st.markdown(f"<div><b>Arabic:</b> {a['text']}</div>", unsafe_allow_html=True) | |
| st.audio(a["audio"]) | |
| if st.button(f"๐ Bookmark Ayah {ayah_number}", key=f"bm{ayah_number}"): | |
| add_bookmark({ | |
| "surah": trans['englishName'], | |
| "ayah": ayah_number, | |
| "arabic": a["text"], | |
| "translation": t["text"] | |
| }) | |
| else: | |
| trans = get_translation(surah_num, ayah_num, lang=lang_code) | |
| arabic = get_arabic_audio(surah_num, ayah_num) | |
| ayah_text = trans['text'].strip() | |
| if ayah_text == BISMILLAH_TEXT and surah_num != 1: | |
| st.warning("This is not a valid Ayah. Please select another.") | |
| else: | |
| st.markdown(f"## {trans['surah']['englishName']} โ Ayah {ayah_num}") | |
| st.markdown(f"<div class='highlight'><b>{ayah_text}</b></div>", unsafe_allow_html=True) | |
| st.markdown(f"<div><b>Arabic:</b> {arabic['text']}</div>", unsafe_allow_html=True) | |
| st.audio(arabic["audio"]) | |
| if st.button("๐ Bookmark this Ayah"): | |
| add_bookmark({ | |
| "surah": trans['surah']['englishName'], | |
| "ayah": ayah_num, | |
| "arabic": arabic["text"], | |
| "translation": ayah_text | |
| }) | |
| st.sidebar.markdown("---") | |
| st.sidebar.markdown("### ๐ Your Bookmarks") | |
| if bookmarks: | |
| for bm in bookmarks: | |
| with st.sidebar.expander(f"{bm['surah']} โ Ayah {bm['ayah']}"): | |
| st.markdown(f"**Arabic:** {bm['arabic']}") | |
| st.markdown(f"**Translation:** {bm['translation']}") | |
| else: | |
| st.sidebar.info("No bookmarks yet.") | |