import datetime import streamlit as st import requests from pydantic import BaseModel 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 import os st.title("Fabrique à documents") fabriquedoc_endpoint = os.environ.get("FABRIQUEDOC_ENDPOINT", "http://0.0.0.0:8051") tab1, tab2, tab3 = st.tabs(["Markdown", "Aperçu", "Paramètres"]) with tab1: content = st.text_area("Enter Markdown text to send:", "# Titre\n## sous-titre\nContenu\n> Citation\n", height=450) 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 linkcolor = st.text_input("Link color:", value="blue") tocdepth = st.number_input("Table of Contents depth:", value=2, min_value=1, max_value=5, step=1) pdfengine = st.text_input("PDF engine:", value="lualatex") fontsize = st.number_input("Font size:", value=14, step=1) paperwidth = st.number_input("Paper width:", value=1080, step=30) paperheight = st.number_input("Paper height:", value=1080, step=30) margin = st.number_input("Margin:", value=180, step=10) vmargin = st.number_input("Vertical margin:", value=180, 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}")