Hello,
I’m trying to create a web form using the Streamlit Python library and Streamlit_drawable_canvas to insert a signature in my “Signature” column, but I can’t get it to work. Can any of you help me?
The image loads properly in the file manager, but then the table is no longer accessible and gives me a message:
“Editor download failed.”
Then blank screen.
import os
import requests
import base64
import numpy as np
from PIL import Image
import io
import streamlit as st
from streamlit_drawable_canvas import st_canvas
from urllib import parse
from datetime import datetime
from seatable_api import Base
# Configuration SeaTable
API_TOKEN = "MYTOKEN"
SERVER_URL = "https://seatable.yblis.fr"
TABLE_NAME = "Livre de Police"
WORKSPACE_ID = 9
FILE_COLUMN_NAME = "Signature"
# Création d'une instance de l'API SeaTable
base = Base(API_TOKEN, SERVER_URL)
# Authentification auprès de SeaTable
base.auth()
# Création du formulaire
st.write("# Signer")
# Définir les dimensions de la zone de dessin
canvas_result = st_canvas(
fill_color="rgba(255, 165, 0, 0.3)",
stroke_width=5,
stroke_color="white",
background_color="rgba(0, 0, 0, 0)",
height=150,
width=500,
drawing_mode="freedraw",
key="canvas",
)
# Afficher la zone de dessin
if canvas_result.image_data is not None:
st.image(canvas_result.image_data)
# Ajout de la ligne dans SeaTable
if st.button("Ajouter"):
# Récupérer les données de l'image au format PNG
image_data = np.array(canvas_result.image_data, dtype=np.uint8, copy=False)
image_data = np.ascontiguousarray(image_data)
image_data = image_data[:, :, :4]
im = Image.fromarray(image_data)
# Convertir l'image en fichier binaire PNG
img_buffer = io.BytesIO()
im.save(img_buffer, format="PNG")
img_buffer.seek(0)
# Upload image to SeaTable
file_name = f'Signature {datetime.now().strftime("%Y-%m-%d_%H-%M-%S")}.png'
relative_path = 'images/' + datetime.now().strftime('%Y-%m')
upload_url_data = base.get_file_upload_link()
upload_url = upload_url_data.get('upload_link') + '?ret-json=1'
parent_dir = upload_url_data.get('parent_path')
data = {
'parent_dir': parent_dir,
'relative_path': relative_path,
}
files = {'file': (file_name, img_buffer)}
resp = requests.post(upload_url, data=data, files=files)
resp_json = resp.json()
name = resp_json[0]['name']
size = resp_json[0]['size']
url = f"{SERVER_URL.rstrip('/')}/workspace/{WORKSPACE_ID}{parent_dir}/{relative_path}/{name}"
file_data = {
'name': name,
'size': size,
'type': 'file',
'url': url,
}
# Ajout de la ligne dans la table
new_row = {FILE_COLUMN_NAME: [file_data]}
try:
response = base.append_row(TABLE_NAME, new_row)
st.success("Nouvelle ligne ajoutée avec succès !")
except Exception as e:
st.error(f"Une erreur s'est produite lors de l'ajout de la ligne : {e}")