def get_default_value(self, name): if get_session_id() not in self.status: ret = None else: ret = self.visita_ricorsiva_dict(self.status[get_session_id()], name) #gestione delle date if isinstance(ret, str) and "datetime" in ret: return date.fromisoformat(ret.split("+")[1]) #gestione delle selectbox if name in self.dict_indexed[get_session_id()]: ret = self.get_indexed_value(name, ret) return ret
def salva_sessione(self): dflt = ''.join( random.choice(string.ascii_uppercase + string.digits) for _ in range(6)) if st.button("SALVA SESSIONE"): st.write("Usa il seguente codice per condividere la tua sessione") st.code(dflt) filename = os.path.join(os.curdir, "json", str(get_session_id())) with open(filename, "r") as fp1: data = json.load(fp1) #tolgo le info sugli screen che non mi interessano for key in list(data.keys()): if key != data["Scelta screen"] and key != "Scelta screen": del data[key] if "log.json" in os.listdir(os.curdir): with open("log.json", "r") as fp2: total = json.load(fp2) total[dflt] = data with open("log.json", "w") as fp2: fp2.write(json.dumps(total)) else: with open("log.json", "w") as fp2: fp2.write(json.dumps({dflt: data})) st.success("Sessione salvata correttamente")
def __init__(self, state): self.id = get_session_id() self.state = state #serve a fare match indice_valore self.dict_indexed = {} self.status = None
def __init__(self, name, screen_name = None, chart_name = None): self.name = name if len(name) == 1 and name[list(name.keys())[0]] == "Prova": return filename = os.path.join(os.curdir, "json", str(get_session_id())) dir = os.listdir(os.path.join(os.curdir, "json")) if get_session_id() in dir: with open(filename, "r") as fp: data = json.load(fp) for key in name: if screen_name is None: #è un widget del main data[key] = name[key] elif chart_name is None: #è un widget dello screen if screen_name not in data: data[screen_name] = {} data[screen_name][key] = name[key] else: if screen_name not in data: data[screen_name] = {} if chart_name not in data[screen_name]: data[screen_name][chart_name] = {} data[screen_name][chart_name][key] = name[key] with open(filename, "w") as fp: fp.write(json.dumps(data, default=self.default)+"\n") else: with open(filename, "w") as fp: fp.write(json.dumps(name, default=self.default)+"\n")
def recupera_sessione(self): scelta = st.text_input("Inserisci il codice") if st.button("RECUPERA"): #controllo che esista trovato = False with open("log.json", "r") as fp: data = json.load(fp) if scelta in data: trovato = True if trovato: self.status[get_session_id()] = data[scelta] st.experimental_rerun() else: st.error("Sessione inesistente")
def add_indexed(self, name, value): if get_session_id() not in self.dict_indexed: self.dict_indexed[get_session_id()] = {} if name not in self.dict_indexed[get_session_id()] or len( self.dict_indexed[get_session_id()][name]) < len(value): self.dict_indexed[get_session_id()][name] = value
def get_indexed_value(self, name, value): if value is None: return 0 return self.dict_indexed[get_session_id()][name].index(value)
def show(self): self.show_heading() dflt = ''.join( random.choice(string.ascii_uppercase + string.digits) for _ in range(6)) col1, col2 = st.beta_columns(2) filename = os.path.join(os.curdir, "json", str(get_session_id())) with col1: st.write( "**Premi per salvare la sessione, ti verrà fornito un codice:**" ) with col2: if st.button("SALVA SESSIONE BIS"): st.code(dflt) with open(filename, "r") as fp1: data = json.load(fp1) #tolgo le info sugli screen che non mi interessano for key in list(data.keys()): st.write(key) if key != data[ "Scelta screen"] and key != "Scelta screen": del data[key] st.write("RIMOSSA") if "log.json" in os.listdir(os.curdir): with open("log.json", "r") as fp2: total = json.load(fp2) total[dflt] = data with open("log.json", "w") as fp2: fp2.write(json.dumps(total)) else: with open("log.json", "w") as fp2: fp2.write(json.dumps({dflt: data})) col1, col2 = st.beta_columns(2) with col1: st.write("**Inserisci il codice per recuperare la sessione:**") with col2: scelta = st.text_input("") rec = st.button("RECUPERA SESSIONE BIS") if rec: #controllo che esista trovato = False with open("log.json", "r") as fp: data = json.load(fp) if scelta in data: trovato = True if trovato: st.success("Sessione esistente") st.write(data[scelta]) r.status = data[scelta] raise st.script_runner.RerunException( st.script_request_queue.RerunData(None)) else: st.error("Sessione inesistente") with st.beta_expander("Vedi elenco sessioni"): st.success("Elenco sessioni") with open("log.json", "r") as fp: for line in fp: st.write(line) with open(filename, "r") as fp: data = json.load(fp) st.write(data) if st.button("Pulisci il file di log"): os.remove("log.json")