halimbahae commited on
Commit
a069add
·
verified ·
1 Parent(s): 5a8fbbb

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +119 -0
app.py ADDED
@@ -0,0 +1,119 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ import requests
4
+ from wordcloud import WordCloud
5
+ import matplotlib.pyplot as plt
6
+
7
+ # Define array of file paths for each book
8
+ book_files = {
9
+ "Maliks_Muwataa": [
10
+ "maliks_muwataa_ahadith.utf8.csv",
11
+ "maliks_muwataa_ahadith_mushakkala_mufassala.utf8.csv"
12
+ ],
13
+ "Musnad_Ahmad_Ibn-Hanbal": [
14
+ "musnad_ahmad_ibn-hanbal_ahadith.utf8.csv",
15
+ "musnad_ahmad_ibn-hanbal_ahadith_mushakkala.utf8.csv"
16
+ ],
17
+ "Sahih_Al-Bukhari": [
18
+ "sahih_al-bukhari_ahadith.utf8.csv",
19
+ "sahih_al-bukhari_ahadith_mushakkala_mufassala.utf8.csv"
20
+ ],
21
+ "Sahih_Muslim": [
22
+ "sahih_muslim_ahadith.utf8.csv",
23
+ "sahih_muslim_ahadith_mushakkala_mufassala.utf8.csv"
24
+ ],
25
+ "Sunan_Abu-Dawud": [
26
+ "sunan_abu-dawud_ahadith.utf8.csv",
27
+ "sunan_abu-dawud_ahadith_mushakkala_mufassala.utf8.csv"
28
+ ],
29
+ "Sunan_Al-Darimi": [
30
+ "sunan_al-darimi_ahadith.utf8.csv",
31
+ "sunan_al-darimi_ahadith_mushakkala_mufassala.utf8.csv"
32
+ ],
33
+ "Sunan_Al-Nasai": [
34
+ "sunan_al-nasai_ahadith.utf8.csv",
35
+ "sunan_al-nasai_ahadith_mushakkala_mufassala.utf8.csv"
36
+ ],
37
+ "Sunan_Al-Tirmidhi": [
38
+ "sunan_al-tirmidhi_ahadith.utf8.csv",
39
+ "sunan_al-tirmidhi_ahadith_mushakkala_mufassala.utf8.csv"
40
+ ],
41
+ "Sunan_Ibn-Maja": [
42
+ "sunan_ibn-maja_ahadith.utf8.csv",
43
+ "sunan_ibn-maja_ahadith_mushakkala_mufassala.utf8.csv"
44
+ ]
45
+ }
46
+
47
+ # Main Streamlit app
48
+ def main():
49
+ st.title("Hadith Viewer")
50
+
51
+ st.sidebar.title("Navigation")
52
+ st.sidebar.subheader("Actions")
53
+ if st.sidebar.button("Home"):
54
+ display_home()
55
+ if st.sidebar.button("Word Cloud"):
56
+ display_word_cloud()
57
+
58
+ st.sidebar.title("Books")
59
+ selected_book = st.sidebar.selectbox("Select a Book", list(book_files.keys()))
60
+
61
+ selected_files = book_files[selected_book]
62
+ selected_file = st.sidebar.selectbox("Select a File", selected_files)
63
+
64
+ # Load CSV file immediately when selected from the list
65
+ file_url = f"https://raw.githubusercontent.com/halimbahae/Hadith/main/{selected_book}/{selected_file}"
66
+ csv_df = pd.read_csv(file_url, header=None) # Assuming no header in CSV files
67
+
68
+ if csv_df is not None:
69
+ # Display dataframe with search
70
+ display_table(csv_df)
71
+
72
+ def display_home():
73
+ st.title("Hadith Viewer")
74
+ st.image("https://raw.githubusercontent.com/halimbahae/Hadith/main/Hadith_Books.jpg", caption="Hadith Books", use_column_width=True)
75
+ 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.")
76
+
77
+ def display_table(csv_df):
78
+ font_size = st.slider("Adjust Font Size", min_value=10, max_value=30, value=20)
79
+ st.write("### Table View")
80
+ search_query = st.sidebar.text_input("Search", "")
81
+ filtered_df = csv_df[csv_df.apply(lambda row: row.astype(str).str.contains(search_query, case=False).any(), axis=1)]
82
+ styled_df = (
83
+ filtered_df.style
84
+ .set_properties(**{'font-size': f'{font_size}px'})
85
+ .set_table_styles([{'selector': 'th', 'props': [('font-size', f'{font_size}px')]},
86
+ {'selector': 'td', 'props': [('font-size', f'{font_size}px'), ('line-height', '1.5')]}])
87
+ )
88
+ st.dataframe(styled_df)
89
+
90
+ def display_word_cloud():
91
+ st.title("Word Cloud")
92
+ st.write("Displaying the top 10 words from the selected CSV file.")
93
+
94
+ selected_book = st.sidebar.selectbox("Select a Book", list(book_files.keys()))
95
+ selected_files = book_files[selected_book]
96
+ selected_file = st.sidebar.selectbox("Select a File", selected_files)
97
+
98
+ file_url = f"https://raw.githubusercontent.com/halimbahae/Hadith/main/{selected_book}/{selected_file}"
99
+ csv_df = pd.read_csv(file_url, header=None)
100
+
101
+ if csv_df is not None:
102
+ word_counts = {}
103
+ for row in csv_df.itertuples(index=False):
104
+ for word in str(row[1]).split():
105
+ if word in word_counts:
106
+ word_counts[word] += 1
107
+ else:
108
+ word_counts[word] = 1
109
+
110
+ top_words = sorted(word_counts, key=word_counts.get, reverse=True)[:10]
111
+
112
+ wordcloud = WordCloud(width=800, height=400, background_color='white').generate(' '.join(top_words))
113
+ plt.figure(figsize=(10, 5))
114
+ plt.imshow(wordcloud, interpolation='bilinear')
115
+ plt.axis('off')
116
+ st.pyplot()
117
+
118
+ if __name__ == "__main__":
119
+ main()