examdownload / app.py
Lenylvt's picture
Update app.py
ee24ba8 verified
import streamlit as st
import os
from streamlit_pdf_viewer import pdf_viewer
# Define the base directory where the folders are located
BASE_DIR = "./brevet"
# Utility function to list files for a given subject and type
def get_files(subject, file_type):
subject_path = os.path.join(BASE_DIR, subject)
# Adjust to remove '.pdf' from filenames
if file_type == 'Corrigé':
files = [f.replace(' Corrigé.pdf', '') for f in os.listdir(subject_path) if 'Corrigé' in f]
else:
files = [f.replace('.pdf', '') for f in os.listdir(subject_path) if 'Corrigé' not in f and f.endswith('.pdf')]
return sorted(files, reverse=True)
# Streamlit app layout
st.title("Sujets et Corrigés du Brevet (*Par Studyrama*)")
# Subject selection
subject = st.selectbox("Sélectionner la matière", ["Francais", "Mathématique", "Science", "Histoire-Géographie-EMC"])
# File type selection (Sujet or Corrigé)
file_type = st.selectbox("Sélectionner le type", ["Sujet", "Corrigé"])
# Year and File selection based on the subject and file type
files = get_files(subject, file_type)
selected_file = st.selectbox("Sélectionner l'année", files)
file_name = f"{selected_file}{' Corrigé' if file_type == 'Corrigé' else ''}.pdf"
file_path = os.path.join(BASE_DIR, subject, file_name)
# Initialize or update session state for button press
if 'show_pdf' not in st.session_state:
st.session_state.show_pdf = False
# Create columns for the PDF viewer and download button
col1, col2 = st.columns(2)
with col1:
if st.button(f"Afficher {file_type}"):
st.session_state.show_pdf = True
if st.session_state.show_pdf:
pdf_viewer(file_path, width=700) # Adjust width as needed
with col2:
with open(file_path, "rb") as pdf_file:
st.download_button(label=f"Télécharger {file_type}",
data=pdf_file,
file_name=file_name,
mime="application/pdf")