File size: 1,728 Bytes
1e0e995
 
 
276509e
 
 
 
 
 
1e0e995
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
276509e
 
 
1e0e995
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import tempfile
import json
import time

#st.write("The warning is cleared in 3 seconds!")
#alert = st.warning("Warning!") # Display the alert
#time.sleep(3) # Wait for 3 seconds
#alert.empty() 

def add_upload(choice):
    """
    Provdies the user with choice to either 'Upload Document' or 'Try Example'.
    Based on user choice runs streamlit processes and save the path and name of
    the 'file' to streamlit session_state which then can be fetched later.

    """
    
    if choice == 'Upload Document':
        
       # if 'filename' in st.session_state:
          # Delete all the items in Session state
        #    for key in st.session_state.keys():
         #       del st.session_state[key]

        uploaded_file = st.sidebar.file_uploader('Upload the File',
                            type=['pdf', 'docx', 'txt'])
        if uploaded_file is not None:
            with tempfile.NamedTemporaryFile(mode="wb", delete = False) as temp:
                bytes_data = uploaded_file.getvalue()
                temp.write(bytes_data)
                st.session_state['filename'] = uploaded_file.name
                st.session_state['filepath'] = temp.name
            succes  = st.success("Upload succesful")
            time.sleep(3)
            succes.empty()

                
    else:
        # listing the options
        with open('docStore/sample/files.json','r') as json_file:
            files = json.load(json_file)

        option = st.sidebar.selectbox('Select the example document',
                              list(files.keys()))
        file_name = file_path  = files[option]
        st.session_state['filename'] = file_name
        st.session_state['filepath'] = file_path