fabriquedoc-frontend/main.py

186 lines
6.8 KiB
Python
Raw Normal View History

2023-01-05 02:23:08 +00:00
import datetime
import streamlit as st
import requests
from pydantic import BaseModel
import mdformat
import os
2023-01-05 02:23:08 +00:00
class DocumentSpecs(BaseModel):
format: str
style: str
linkcolor: str
tocdepth: int
pdfengine: str
content: str
fontsize: int
paperwidth: int
paperheight: int
2023-02-13 01:44:50 +00:00
ratio: int
2023-01-05 02:23:08 +00:00
margin: int
vmargin: int
extension: str
2023-02-13 01:44:50 +00:00
fps: int
stilltime: int
2023-01-05 02:23:08 +00:00
if 'options' not in st.session_state:
st.session_state['options'] = ""
2023-01-05 02:23:08 +00:00
st.title("Fabrique à documents")
2023-01-05 03:17:34 +00:00
fabriquedoc_endpoint = os.environ.get("FABRIQUEDOC_ENDPOINT", "http://0.0.0.0:8000")
2023-01-05 02:23:08 +00:00
2023-07-05 05:48:59 +00:00
tab1, tab2, tab3, tab4 = st.tabs(["Markdown", "Aperçu", "Paramètres", "Images"])
2023-01-05 02:23:08 +00:00
with tab1:
def button1_callback():
st.session_state['markdown'] = mdformat.text(st.session_state['markdown'],
options={"number": True})
2023-02-13 01:44:50 +00:00
content = st.text_area("Enter Markdown text to send:",
2023-07-05 05:48:59 +00:00
"""
# Ceci est un titre\n
## Ceci est un sous-titre\n\n
Ceci est un paragraphe\n\n
## Ceci est un autre sous-titre\n\n
> Ceci est du code\n\n
Ceci est un emoji :heart_eyes:\n\n
::: {.center}\nCeci est centré\n:::
""",
height=450,
key='markdown')
st.button("Formater le texte", on_click=button1_callback)
2023-01-05 02:23:08 +00:00
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)
2023-01-05 02:23:08 +00:00
# Autres paramètres
2023-02-13 01:44:50 +00:00
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)
2023-02-13 01:44:50 +00:00
ratio = st.number_input("Ratio:",
value=int(format_parameters.get("ratio")),
step=10)
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)
2023-02-13 01:44:50 +00:00
extension = st.selectbox("Extension:", ["png", "jpg", "pdf", "mp4"])
if extension == "mp4":
fps = st.number_input("FPS:",
value=int(format_parameters.get("fps")),
step=1)
stilltime = st.number_input("Still time:",
value=int(format_parameters.get("stilltime")),
step=1)
else:
fps = 0
stilltime = 0
2023-01-05 02:23:08 +00:00
# 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,
2023-02-13 01:44:50 +00:00
ratio=ratio,
2023-01-05 02:23:08 +00:00
margin=margin,
vmargin=vmargin,
2023-02-13 01:44:50 +00:00
extension=extension,
fps=fps,
stilltime=stilltime
2023-01-05 02:23:08 +00:00
)
headers = {"Content-Type": "application/json"}
# Send the POST request with the JSON data in the request body
2023-07-05 05:48:59 +00:00
response = requests.get(f"{fabriquedoc_endpoint}/generer/",
2023-07-05 05:49:51 +00:00
json=document_specs.model_dump_json(),
2023-07-05 05:48:59 +00:00
headers=headers)
2023-01-05 02:23:08 +00:00
# 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}")
2023-07-05 05:48:59 +00:00
with tab4:
st.header("Images")
st.write("Uploaded image")
# list uploaded files with a request to the GET /images endpoint
response = requests.get(f"{fabriquedoc_endpoint}/images/")
images = response.json()["images"]
selected_image = st.selectbox("Select an image:", images)
image_response = requests.get(f"{fabriquedoc_endpoint}/images/{selected_image}")
image_data = image_response.content
st.image(image_data)
st.write("Upload image")
uploaded_files = st.file_uploader("Choose an image file", type=["png", "jpg", "jpeg"])
if uploaded_files is not None:
url = f"{fabriquedoc_endpoint}/images/"
# Create a FormData object
files = {"file": uploaded_files}
# Submit the file to the endpoint
response = requests.post(url, files=files)
# Check the response status
if response.status_code < 300:
st.write("File sent successfully!")
else:
st.write("File upload failed.")