Spaces:
Running
Running
File size: 4,299 Bytes
2c2081e de6562c 2c2081e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
"""Fetch upload and convert to list1/list2."""
# pylint: disable=too-many-locals, too-many-branches, too-many-statements
import streamlit as st
from logzero import logger
from streamlit import session_state as state
def fetch_upload():
"""Fetch upload and convert to list1/list2."""
src_fileio = b""
tgt_fileio = b""
with st.form(key="upload_in_form"):
_ = st.expander(f"{state.ns.beetype}: Pick two files", expanded=True)
with _:
col1, col2 = st.columns(2)
with col1:
src_fileio = st.file_uploader(
"Choose source file (utf8 txt)",
type=[
"txt",
],
key="src_text",
# accept_multiple_files=True,
# accept_multiple_files=False,
)
with col2:
tgt_fileio = st.file_uploader(
"Choose target file (utf8 txt)",
type=[
"txt",
],
key="tgt_text",
# accept_multiple_files=True,
)
submitted = st.form_submit_button("Submit")
# logger.debug(" len(src_fileio): %s", len(src_fileio))
# logger.debug(" len(tgt_fileio): %s", len(tgt_fileio))
filename1 = ""
if src_fileio:
logger.debug(" type(src_fileio): %s", type(src_fileio))
# for st.file_uploade accept_multiple_files=True
if isinstance(src_fileio, list):
logger.debug(" len(src_fileio): %s", len(src_fileio))
filenames = []
try:
filenames = [elm.name for elm in src_fileio] # type: ignore
except Exception as exc:
logger.error(exc)
logger.debug("src_fileio names: *%s*", filenames)
# state.ns.src_fileio = src_fileio
state.ns.src_file = src_fileio[-1].getvalue().decode()
state.ns.src_filename = src_fileio[-1].name
else:
logger.debug("src_fileio.name: [%s]", src_fileio.name)
filenames = [src_fileio.name]
logger.debug("src_fileio names: %s", filenames)
# state.ns.src_fileio = src_fileio
state.ns.src_file = src_fileio.getvalue().decode()
state.ns.src_filename = src_fileio.name
filename1 = state.ns.src_filename
filename2 = ""
if tgt_fileio:
if isinstance(tgt_fileio, list):
logger.warning("not set to handle multiple files")
logger.warning("set accept_multiple_files=False in the meantime")
else:
state.ns.tgt_file = tgt_fileio.getvalue().decode()
state.ns.tgt_filename = tgt_fileio.name
filename2 = tgt_fileio.name
# proceed when Submit is clicked
msg1 = ""
if filename1:
msg1 += f" file1 {filename1}"
msg2 = ""
if filename2:
msg2 += f" file2 {filename2}"
glue = ""
if filename1 and filename2:
glue = ", "
upload_placeholder = st.empty()
prefix = f" Upload submitted: {msg1}{glue}{msg2}"
upload_placeholder.write(prefix)
# st.write(f" Submitted upload: {msg1}{glue}{msg2}")
if not submitted:
return None
if not (filename1 or filename2):
# st.write("| no file uploaded")
upload_placeholder.write(f"{prefix} no file uploaded")
return None
if not filename1:
# st.write("| file1 not ready")
upload_placeholder.write(f"{prefix}, file1 not ready")
return None
if not filename2:
# st.write("| file2 not ready")
upload_placeholder.write(f"{prefix}, file2 not ready")
return None
try:
_ = state.ns.src_file.splitlines()
list1 = [elm.strip() for elm in _ if elm.strip()]
_ = state.ns.tgt_file.splitlines()
list2 = [elm.strip() for elm in _ if elm.strip()]
except Exception as exc:
logger.error(exc)
list1 = [""]
list2 = [""]
logger.debug("len(list1): %s, len(list2): %s", len(list1), len(list2))
state.ns.list1 = list1[:]
state.ns.list2 = list2[:]
state.ns.updated = True
logger.debug("state.ns.updated: %s", state.ns.updated)
return None
|