There were previous discussions about creating a file picker for the local system. These ultimately produced a nice file upload capability but not a file picker, which I needed. I hacked together a solution that works for me though it is pretty rough given my long absence from using Python. It depends on the SessionState code, too.
I’d really like to see this capability built in, of course, but this works for now.
#!/usr/bin/env python
import streamlit as st
import pandas as pd
import os
from os import walk
import SessionState
def getfiles(folder_path):
files = [f for f in os.listdir(folder_path) if f.endswith(’.json’)]
return (files)
def file_selector(folder_path):
filenames = os.listdir(folder_path)
selected_filename = st.selectbox(‘Select a file’, filenames)
return os.path.join(folder_path, selected_filename)
def getdata(path):
datafile = json.load(open(path))
dataframe = pd.DataFrame(datafile[“rows”])
return (dataframe)
def main():
session_state = SessionState.get(working_file="", load_button=False,
df=pd.DataFrame(), meta=pd.DataFrame, title="Please load a file")
dir = '../Output'
files = getfiles(dir)
working_file = st.selectbox('Select file to visualize', files)
load_button = st.button("Load file")
if load_button or session_state.load_button:
session_state.load_button = True
tf = getdata(dir + '/' + working_file)
session_state.df = tf.copy()
session_state.df['file'] = working_file
session_state.title = working_file
workingfile = ""
load_button = ''
session_state.load_button = False
st.title(session_state.title)
expander = st.beta_expander("Analyst comments")
expander.write("Place comments here")
if session_state.title != "Please load a file":
#
# Start plotting
#