2023-01-05 02:23:08 +00:00
import datetime
import streamlit as st
import requests
from pydantic import BaseModel
2023-01-22 00:02:35 +00:00
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
2023-01-22 00:02:35 +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
tab1 , tab2 , tab3 = st . tabs ( [ " Markdown " , " Aperçu " , " Paramètres " ] )
with tab1 :
2023-01-22 00:02:35 +00:00
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: " ,
" # 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} \n Ceci est centré \n ::: " ,
2023-01-22 00:02:35 +00:00
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-22 00:02:35 +00:00
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 } " )
2023-01-22 00:02:35 +00:00
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 )
2023-01-22 00:02:35 +00:00
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
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 } " )