This repository has been archived on 2025-01-03. You can view files and clone it, but cannot push or open issues or pull requests.
fabriquedoc-frontend/app_tab3.py
2024-11-24 21:40:53 -05:00

96 lines
No EOL
4.4 KiB
Python

import datetime
import requests
import streamlit as st
from models import DocumentSpecs
def app_tab3():
st.header("Paramètres")
http_headers = {"Authorization": f"Bearer {st.session_state['bearer_token']}"}
# Styles
response_styles = requests.get(f"{st.session_state['fabriquedoc_endpoint']}/styles/", headers=http_headers).json()
styles = response_styles.get("styles")
selected_style = st.selectbox("Select a style:", styles)
# Formats
response_formats = requests.get(f"{st.session_state['fabriquedoc_endpoint']}/formats/{selected_style}/", headers=http_headers).json()
formats = response_formats.get("formats")
selected_format = st.selectbox("Select a format:", formats)
# Autres paramètres
response_format_parameters = requests.get(
f"{st.session_state['fabriquedoc_endpoint']}/format_parameters/{selected_style}/{selected_format}/", headers=http_headers).json()
linkcolor = st.text_input("Link color:",
value=response_format_parameters.get("linkcolor"))
tocdepth = st.number_input("Table of Contents depth:",
value=int(response_format_parameters.get("tocdepth")),
min_value=1,
max_value=5,
step=1)
pdfengine = st.text_input("PDF engine:",
value=response_format_parameters.get("pdfengine"))
fontsize = st.number_input("Font size:",
value=int(response_format_parameters.get("fontsize")),
step=1)
paperwidth = st.number_input("Paper width:",
value=int(response_format_parameters.get("paperwidth")),
step=30)
paperheight = st.number_input("Paper height:",
value=int(response_format_parameters.get("paperheight")),
step=30)
ratio = st.number_input("Ratio:",
value=int(response_format_parameters.get("ratio")),
step=10)
margin = st.number_input("Margin:",
value=int(response_format_parameters.get("margin")),
step=10)
vmargin = st.number_input("Vertical margin:",
value=int(response_format_parameters.get("vmargin")),
step=10)
extension = "mp4"
fps = st.number_input("FPS:",
value=int(response_format_parameters.get("fps")),
step=1)
stilltime = st.number_input("Still time:",
value=int(response_format_parameters.get("stilltime")),
step=1)
# Envoi
if st.button("Generate post"):
document_specs = DocumentSpecs(
format=selected_format,
style=selected_style,
linkcolor=linkcolor,
tocdepth=tocdepth,
pdfengine=pdfengine,
content=st.session_state['content'],
fontsize=fontsize,
paperwidth=paperwidth,
paperheight=paperheight,
ratio=ratio,
margin=margin,
vmargin=vmargin,
extension=extension,
fps=fps,
stilltime=stilltime
)
post_headers = http_headers | {"Content-Type": "application/json"}
# Send the POST request with the JSON data in the request body
response = requests.post(f"{st.session_state['fabriquedoc_endpoint']}/generer/",
json=document_specs.model_dump(),
headers=post_headers,
timeout=(30, 3000)) # 30 seconds connect timeout, 5 minutes read timeout
# 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")
file_name = f"{document_specs.style}-{document_specs.format}-{datef}-output.zip"
st.download_button('Télécharger', 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}")