Ajout de Oauth2

This commit is contained in:
François Pelletier 2024-08-24 00:26:59 -04:00
parent 2478d41729
commit 9857d6fb21
2 changed files with 27 additions and 13 deletions

36
main.py
View file

@ -64,7 +64,20 @@ fabriquedoc_endpoint = os.environ.get("FABRIQUEDOC_ENDPOINT", "http://127.0.0.1:
st.write(f"Endpoint : {fabriquedoc_endpoint}")
tab1, tab2, tab3, tab4 = st.tabs(["Markdown", "Aperçu", "Paramètres", "Images"])
tab0, tab1, tab2, tab3, tab4 = st.tabs(["Login", "Markdown", "Aperçu", "Paramètres", "Images"])
with tab0:
with st.form(key='authentication'):
username = st.text_input("Username")
password = st.text_input("Password", type="password")
submit_button = st.form_submit_button(label='Login')
if submit_button:
# Send a POST request to your authentication endpoint
response = requests.post(f'{fabriquedoc_endpoint}/token/', data={"username": username, "password": password})
if response.status_code == 200:
bearer_token = response.json()["access_token"]
# Store the bearer token in the session state
st.session_state['bearer_token'] = bearer_token
with tab1:
def button1_callback():
@ -83,18 +96,19 @@ with tab2:
with tab3:
st.header("Paramètres")
http_headers = {"Authorization": f"Bearer {st.session_state['bearer_token']}"}
# Styles
response_styles = requests.get(f"{fabriquedoc_endpoint}/styles/").json()
response_styles = requests.get(f"{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"{fabriquedoc_endpoint}/formats/{selected_style}/").json()
response_formats = requests.get(f"{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"{fabriquedoc_endpoint}/format_parameters/{selected_style}/{selected_format}/").json()
f"{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:",
@ -154,11 +168,11 @@ with tab3:
stilltime=stilltime
)
headers = {"Content-Type": "application/json"}
post_headers = http_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.model_dump(),
headers=headers)
response = requests.post(f"{fabriquedoc_endpoint}/generer/",
json=document_specs.model_dump(),
headers=http_headers)
# Check the response status code
if 200 <= response.status_code <= 299:
# If the request is successful, get the file data from the response
@ -182,10 +196,10 @@ with tab4:
st.write("Images disponibles")
# list uploaded files with a request to the GET /images endpoint
response = requests.get(f"{fabriquedoc_endpoint}/images/")
response = requests.get(f"{fabriquedoc_endpoint}/images/", headers=http_headers)
images = response.json()["images"]
selected_image = st.selectbox("Choisis une image:", images)
image_response = requests.get(f"{fabriquedoc_endpoint}/images/{selected_image}")
image_response = requests.get(f"{fabriquedoc_endpoint}/images/{selected_image}", headers=http_headers)
image_data = image_response.content
st.image(image_data)
@ -200,7 +214,7 @@ with tab4:
files = {"file": uploaded_file}
# Submit the file to the endpoint
response = requests.post(url, files=files)
response = requests.post(url, files=files, headers=http_headers)
# Check the response status
if response.status_code < 300:

View file

@ -1,5 +1,5 @@
streamlit~=1.30.0
requests~=2.31.0
pydantic~=2.0.1
mdformat~=0.7.16
pydantic~=2.0.3
mdformat~=0.7.17
python-dotenv~=1.0.1