import datetime import streamlit as st import requests from pydantic import BaseModel import mdformat import os class DocumentSpecs(BaseModel): format: str style: str linkcolor: str tocdepth: int pdfengine: str content: str fontsize: int paperwidth: int paperheight: int margin: int vmargin: int extension: str if 'options' not in st.session_state: st.session_state['options'] = "" st.title("Fabrique à documents") fabriquedoc_endpoint = os.environ.get("FABRIQUEDOC_ENDPOINT", "http://0.0.0.0:8000") tab1, tab2, tab3 = st.tabs(["Markdown", "Aperçu", "Paramètres"]) with tab1: def button1_callback(): st.session_state['markdown'] = mdformat.text(st.session_state['markdown'], options={"number": True}) content = st.text_area("Enter Markdown text to send:", "# Titre\n## sous-titre\nContenu\n> Citation\n", height=450, key='markdown') st.button("Formater le texte", on_click=button1_callback) with tab2: st.write("Aperçu") st.markdown(content) with tab3: st.header("Paramètres") # Styles response_styles = requests.get(f"{fabriquedoc_endpoint}/styles") styles = response_styles.json()["styles"] selected_style = st.selectbox("Select a style:", styles) # Formats response_formats = requests.get(f"{fabriquedoc_endpoint}/formats/{selected_style}/") formats = response_formats.json()["formats"] selected_format = st.selectbox("Select a format:", formats) # Autres paramètres response_format_parameters = requests.get(f"{fabriquedoc_endpoint}/format_parameters/{selected_style}/{selected_format}") format_parameters = response_format_parameters.json() st.write(format_parameters) linkcolor = st.text_input("Link color:", value=format_parameters.get("linkcolor")) tocdepth = st.number_input("Table of Contents depth:", value=int(format_parameters.get("tocdepth")), min_value=1, max_value=5, step=1) pdfengine = st.text_input("PDF engine:", value=format_parameters.get("pdfengine")) fontsize = st.number_input("Font size:", value=int(format_parameters.get("fontsize")), step=1) paperwidth = st.number_input("Paper width:", value=int(format_parameters.get("paperwidth")), step=30) paperheight = st.number_input("Paper height:", value=int(format_parameters.get("paperheight")), step=30) margin = st.number_input("Margin:", value=int(format_parameters.get("margin")), step=10) vmargin = st.number_input("Vertical margin:", value=int(format_parameters.get("vmargin")), step=10) extension = st.selectbox("Extension:", ["png", "jpg", "pdf"]) # Envoi if st.button("Generate post"): document_specs = DocumentSpecs( format=selected_format, style=selected_style, linkcolor=linkcolor, tocdepth=tocdepth, pdfengine=pdfengine, content=content, fontsize=fontsize, paperwidth=paperwidth, paperheight=paperheight, margin=margin, vmargin=vmargin, extension=extension ) headers = {"Content-Type": "application/json"} # Send the POST request with the JSON data in the request body response = requests.get(f"{fabriquedoc_endpoint}/generer/", json=document_specs.dict(), headers=headers) # Check the response status code if 200 <= response.status_code <= 299: # If the request is successful, get the file data from the response file_data = response.content datef = datetime.datetime.now().strftime("%m-%d-%Y") if document_specs.extension in ["jpg", 'png']: extn = "zip" else: extn = document_specs.extension file_name = f"{document_specs.style}-{document_specs.format}-{datef}-output.{extn}" st.download_button('Download File', file_data, file_name=file_name) else: # If the request is unsuccessful, display the error status code and message st.error(f"Request failed with status code {response.status_code}: {response.text}")