Hadith_viewer / app.py
halimbahae's picture
Update app.py
4171868 verified
raw
history blame
4.68 kB
import streamlit as st
import pandas as pd
import requests
from wordcloud import WordCloud
import plotly.express as px
# Define array of file paths for each book
book_files = {
"Maliks_Muwataa": [
"maliks_muwataa_ahadith.utf8.csv",
"maliks_muwataa_ahadith_mushakkala_mufassala.utf8.csv"
],
"Musnad_Ahmad_Ibn-Hanbal": [
"musnad_ahmad_ibn-hanbal_ahadith.utf8.csv",
"musnad_ahmad_ibn-hanbal_ahadith_mushakkala.utf8.csv"
],
"Sahih_Al-Bukhari": [
"sahih_al-bukhari_ahadith.utf8.csv",
"sahih_al-bukhari_ahadith_mushakkala_mufassala.utf8.csv"
],
"Sahih_Muslim": [
"sahih_muslim_ahadith.utf8.csv",
"sahih_muslim_ahadith_mushakkala_mufassala.utf8.csv"
],
"Sunan_Abu-Dawud": [
"sunan_abu-dawud_ahadith.utf8.csv",
"sunan_abu-dawud_ahadith_mushakkala_mufassala.utf8.csv"
],
"Sunan_Al-Darimi": [
"sunan_al-darimi_ahadith.utf8.csv",
"sunan_al-darimi_ahadith_mushakkala_mufassala.utf8.csv"
],
"Sunan_Al-Nasai": [
"sunan_al-nasai_ahadith.utf8.csv",
"sunan_al-nasai_ahadith_mushakkala_mufassala.utf8.csv"
],
"Sunan_Al-Tirmidhi": [
"sunan_al-tirmidhi_ahadith.utf8.csv",
"sunan_al-tirmidhi_ahadith_mushakkala_mufassala.utf8.csv"
],
"Sunan_Ibn-Maja": [
"sunan_ibn-maja_ahadith.utf8.csv",
"sunan_ibn-maja_ahadith_mushakkala_mufassala.utf8.csv"
]
}
# Main Streamlit app
def main():
st.title("Hadith Viewer")
st.sidebar.title("Navigation")
st.sidebar.subheader("Actions")
if st.sidebar.button("Home"):
display_home()
if st.sidebar.button("Word Cloud"):
display_word_cloud()
st.sidebar.title("Books")
selected_book = st.sidebar.selectbox("Select a Book", list(book_files.keys()))
selected_files = book_files[selected_book]
selected_file = st.sidebar.selectbox("Select a File", selected_files)
# Load CSV file immediately when selected from the list
file_url = f"https://raw.githubusercontent.com/halimbahae/Hadith/main/{selected_book}/{selected_file}"
csv_df = pd.read_csv(file_url, header=None) # Assuming no header in CSV files
if csv_df is not None:
# Display dataframe with search
display_table(csv_df)
def display_home():
st.title("Hadith Viewer")
st.image("https://raw.githubusercontent.com/halimbahae/Hadith/main/Hadith_Books.jpg", caption="Hadith Books", use_column_width=True)
st.write("Welcome to the Hadith Viewer! This is a viewer for the Hadith collections. You can select a book from the dropdown menu on the left to view its contents.")
def display_table(csv_df):
font_size = st.slider("Adjust Font Size", min_value=10, max_value=30, value=20, key="font_size_slider")
st.write("### Table View")
search_query = st.sidebar.text_input("Search", "", key="search_input")
filtered_df = csv_df[csv_df.apply(lambda row: row.astype(str).str.contains(search_query, case=False).any(), axis=1)]
styled_df = (
filtered_df.style
.set_properties(**{'font-size': f'{font_size}px'})
.set_table_styles([{'selector': 'th', 'props': [('font-size', f'{font_size}px')]},
{'selector': 'td', 'props': [('font-size', f'{font_size}px'), ('line-height', '1.5')]}])
)
st.dataframe(styled_df)
def display_word_cloud():
st.title("Word Cloud")
st.write("Select the number of words to display.")
selected_book = st.sidebar.selectbox("Select a Book", list(book_files.keys()), key="word_cloud_book_select")
selected_files = book_files[selected_book]
selected_file = st.sidebar.selectbox("Select a File", selected_files, key="word_cloud_file_select")
file_url = f"https://raw.githubusercontent.com/halimbahae/Hadith/main/{selected_book}/{selected_file}"
csv_df = pd.read_csv(file_url, header=None)
if csv_df is not None:
word_counts = {}
for row in csv_df.itertuples(index=False):
for word in str(row[1]).split():
if word in word_counts:
word_counts[word] += 1
else:
word_counts[word] = 1
top_words = sorted(word_counts, key=word_counts.get, reverse=True)[:st.slider("Select Number of Words", min_value=10, max_value=50, value=10, key="word_slider")]
wordcloud_data = {'word': top_words, 'count': [word_counts[word] for word in top_words]}
wordcloud_df = pd.DataFrame(wordcloud_data)
fig = px.bar(wordcloud_df, x='word', y='count', title="Word Cloud")
st.plotly_chart(fig)
if __name__ == "__main__":
main()