Hadith_viewer / app.py
halimbahae's picture
Update app.py
ddc316b verified
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()
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)
if st.button("Word Count"):
display_word_count(filtered_df)
def display_word_count(df):
st.title("Word Count")
st.write("Select the number of words to display and exclude words.")
col1, col2 = st.columns(2)
with col1:
word_count = st.slider("Select Number of Words", min_value=10, max_value=50, value=10, key="word_count_slider")
with col2:
words_to_exclude = st.text_input("Words to Exclude (comma-separated)", "", key="exclude_words_input")
words_to_exclude = [word.strip() for word in words_to_exclude.split(",")]
word_counts = {}
for row in df.itertuples(index=False):
for word in str(row[1]).split():
if word not in words_to_exclude:
if word in word_counts:
word_counts[word] += 1
else:
word_counts[word] = 1
sorted_word_counts = sorted(word_counts.items(), key=lambda x: x[1], reverse=True)[:word_count]
words, counts = zip(*sorted_word_counts)
wordcloud_data = {'word': words, 'count': counts}
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()