def main(): state = SessionState.get(ground_truth=0, detected_patterns=0, true_positive=0, image_num=0, problems=[], dataset_name=None) st.title('Finder Pattern Detector Tester') if state.dataset_name is None: output_dir = Path(__file__).parent.parent / "output" datasets = [dataset.name for dataset in output_dir.iterdir()] dataset_name = st.selectbox('Select Dataset', datasets) if st.button("CONTINUE"): state.dataset_name = dataset_name raise RerunException(RerunData()) else: dataset_path = Path( __file__).parent.parent / "output" / state.dataset_name files = [path for path in dataset_path.iterdir()] if state.image_num < len(files): file = files[state.image_num] st.markdown(f"Dataset: {state.dataset_name}") st.markdown(f"Image: {file.name}") st.image(rescale(Image.open(file))) ground_truth = st.number_input( "Ground Truth Number of Finder Patterns", 0) detected_patterns = st.number_input( "Total Number of Detected Finder Patterns", 0) true_positive = st.number_input( "Number of Correctly Detected Finder Patterns", 0) st.markdown(f"Images to score: {len(files) - state.image_num}") if st.button("NEXT"): state.ground_truth += ground_truth state.detected_patterns += detected_patterns state.true_positive += true_positive state.image_num += 1 if (detected_patterns != true_positive) or (true_positive != ground_truth): state.problems.append(file.name) raise RerunException(RerunData()) else: if state.ground_truth == 0: st.markdown("No images found!") elif state.detected_patterns == 0: st.markdown("No patterns were detected!") else: precision = state.true_positive / state.detected_patterns recall = state.true_positive / state.ground_truth st.markdown(f'**Number of images:** {len(files)}') st.markdown(f'**Precision:** {precision:.2f}') st.markdown(f'**Recall:** {recall:.2f}') if len(state.problems) > 0: st.markdown(f"**Images with problems:**") for img in state.problems: st.markdown(f"- **{img}**") st.markdown(f'Refresh the page to evaluate another dataset')
def main(): df = read_jarchive() state = SessionState.get(question_number=1, num_correct=0, score=0) st.title("Streamlit Jeopardy!") category, question, answer, value = get_one_question( state.question_number, df) answered = False st.write(f"Question from category {category} for ${value}:") st.write(f" {question}") response = st.text_input("What is: ", key=str(state.question_number)) if (response != ''): sresponse = sanitize(response) sanswer = sanitize(answer) if (compare_strings(sresponse, sanswer) >= 0.5): answered = True st.write(f"Correct! The reference answer is {answer}.") else: answered = False st.write( f"Sorry! Your response was {response}. The correct answer is {answer}." ) if (answered): state.num_correct += 1 state.score += value else: state.score -= value st.write( f"Your score is {state.num_correct}/{state.question_number} and winnings are ${state.score}" ) st.write("") if st.button('Next question'): state.question_number += 1 raise RerunException(RerunData(widget_states=None)) if st.button('Reset score'): state.question_number = 0 state.num_correct = 0 state.score = 0 raise RerunException(RerunData(widget_states=None))
def request_rerun(self, client_state: Optional[ClientState]) -> None: """Signal that we're interested in running the script. If the script is not already running, it will be started immediately. Otherwise, a rerun will be requested. Parameters ---------- client_state : streamlit.proto.ClientState_pb2.ClientState | None The ClientState protobuf to run the script with, or None to use previous client state. """ if client_state: rerun_data = RerunData(client_state.query_string, client_state.widget_states) else: rerun_data = RerunData() self._enqueue_script_request(ScriptRequest.RERUN, rerun_data)
def run_labeler(): state = SessionState.get(captcha_number=0) captcha_image_list = get_captcha_list() captcha = get_random_captcha(captcha_image_list, state.captcha_number) st.image(captcha) col_1, col_2 = st.beta_columns(2) if col_1.button('Break it!'): model, lb = import_model('output') image, text = predict_captcha_image(captcha, model, lb) st.image(image) st.write(text) if col_2.button('Next captcha'): state.captcha_number += 1 raise RerunException(RerunData(widget_states=None))
def enqueue_rerun(self, argv=None, widget_states=None): self.script_request_queue.enqueue( ScriptRequest.RERUN, RerunData(widget_states=widget_states))
def rerun(): """Rerun a Streamlit app from the top!""" widget_states = _get_widget_states() raise RerunException(RerunData(widget_states))
def test_rerun_data_coalescing(self): """Test that multiple RERUN requests get coalesced with expected values. (This is similar to widgets_test.test_coalesce_widget_states - it's testing the same thing, but through the ScriptEventQueue interface.) """ queue = ScriptRequestQueue() session_state = SessionState() states = WidgetStates() _create_widget("trigger", states).trigger_value = True _create_widget("int", states).int_value = 123 queue.enqueue(ScriptRequest.RERUN, RerunData(widget_states=states)) states = WidgetStates() _create_widget("trigger", states).trigger_value = False _create_widget("int", states).int_value = 456 session_state.set_metadata( WidgetMetadata("trigger", lambda x, s: x, None, "trigger_value")) session_state.set_metadata( WidgetMetadata("int", lambda x, s: x, lambda x: x, "int_value")) queue.enqueue(ScriptRequest.RERUN, RerunData(widget_states=states)) event, data = queue.dequeue() self.assertEqual(event, ScriptRequest.RERUN) session_state.set_widgets_from_proto(data.widget_states) # Coalesced triggers should be True if either the old or # new value was True self.assertEqual(True, session_state.get("trigger")) # Other widgets should have their newest value self.assertEqual(456, session_state.get("int")) # We should have no more events self.assertEqual((None, None), queue.dequeue(), "Expected empty event queue") # Test that we can coalesce if previous widget state is None queue.enqueue(ScriptRequest.RERUN, RerunData(widget_states=None)) queue.enqueue(ScriptRequest.RERUN, RerunData(widget_states=None)) states = WidgetStates() _create_widget("int", states).int_value = 789 queue.enqueue(ScriptRequest.RERUN, RerunData(widget_states=states)) event, data = queue.dequeue() session_state.set_widgets_from_proto(data.widget_states) self.assertEqual(event, ScriptRequest.RERUN) self.assertEqual(789, session_state.get("int")) # We should have no more events self.assertEqual((None, None), queue.dequeue(), "Expected empty event queue") # Test that we can coalesce if our *new* widget state is None states = WidgetStates() _create_widget("int", states).int_value = 101112 queue.enqueue(ScriptRequest.RERUN, RerunData(widget_states=states)) queue.enqueue(ScriptRequest.RERUN, RerunData(widget_states=None)) event, data = queue.dequeue() session_state.set_widgets_from_proto(data.widget_states) self.assertEqual(event, ScriptRequest.RERUN) self.assertEqual(101112, session_state.get("int")) # We should have no more events self.assertEqual((None, None), queue.dequeue(), "Expected empty event queue")
for col_filter, filter_value in zip(col_filters_list, filters_list): data_filtered = data_filtered[data_filtered[col_filter] == filter_value] return data_filtered if __name__ == '__main__': try: main() except Exception as exception: project_identifier, exception_desc = options_file.project_id, str( sys.exc_info()[1]) log_record('OPR Error - ' + exception_desc, project_identifier, flag=2, solution_type='OPR') error_upload(options_file, project_identifier, format_exc(), exception_desc, error_flag=1, solution_type='OPR') session_state.run_id += 1 st.error( 'AVISO: Ocorreu um erro. Os administradores desta página foram notificados com informação do erro e este será corrigido assim que possível. Entretanto, esta aplicação será reiniciada. Obrigado pela sua compreensão.' ) time.sleep(10) raise RerunException(RerunData())
def main(): data = get_data(options_file) unmatched_data = get_data_non_cached(options_file, 0) matched_data = get_data_non_cached(options_file, 1) sel_goal = st.sidebar.radio('Modo de utilização:', ['Gamas por Corresponder', 'Gamas Mortas Correspondidas', 'Gamas Vivas Correspondidas'], index=0) if sel_goal == 'Gamas por Corresponder': session_state.validate_button_pressed = 0 if not unmatched_data.shape[0]: st.write('Não existem gamas sem correspondência.') return else: sel_brand = st.sidebar.selectbox('Marca:', ['-'] + list(unmatched_data['PT_PDB_Franchise_Desc'].unique()), index=0) if sel_brand != '-': unmatched_data = unmatched_data.loc[unmatched_data['PT_PDB_Franchise_Desc'] == sel_brand.upper(), :] # print('2 - ', unmatched_data.shape) unique_models = [x for x in list(unmatched_data['PT_PDB_Model_Desc'].unique()) if x not in ['H-1', 'H-1 3 lugares', 'H-1 6 lugares', 'H350', 'i20 Coupe', 'i20 VAN']] sel_model = st.sidebar.selectbox('Modelo', ['-'] + unique_models, index=0) # sel_confidence_threshold = st.sidebar.slider('Grau de Semelhança', min_value=0.0, max_value=1.0, value=0.8, step=0.01) if sel_model != '-': if sel_brand != session_state.sel_brand or sel_model != session_state.sel_model: # session_state.sel_brand = sel_brand # session_state.sel_model = sel_model session_state.unmatched_data_filtered = filter_data(unmatched_data, [sel_model], ['PT_PDB_Model_Desc']) matched_data_filtered = filter_data(data, [sel_model, sel_brand], ['PT_PDB_Model_Desc', 'PT_PDB_Franchise_Desc']) st.write('Existem as seguintes gamas por corresponder para a marca {} e modelo {}:'.format(sel_brand, sel_model)) display_gamas_mortas = session_state.unmatched_data_filtered.loc[session_state.unmatched_data_filtered['PT_PDB_Commercial_Version_Flag'] == -1, :]['PT_PDB_Commercial_Version_Desc_Old'].rename('Gama').reset_index(drop=True) if display_gamas_mortas.shape[0]: st.write('Gamas Mortas:') st.table(display_gamas_mortas) display_gamas_vivas = session_state.unmatched_data_filtered.loc[session_state.unmatched_data_filtered['PT_PDB_Commercial_Version_Flag'] == 1, :]['PT_PDB_Commercial_Version_Desc_Old'].rename('Gama').reset_index(drop=True) if display_gamas_vivas.shape[0]: st.write('Gamas Vivas:') st.table(display_gamas_vivas) # session_state.gama_viva_per_model = matched_data_filtered['PT_PDB_Commercial_Version_Desc_New'].unique() session_state.gama_viva_per_model = list(matched_data_filtered.loc[matched_data_filtered['PT_PDB_Commercial_Version_Flag'] == 1, 'PT_PDB_Commercial_Version_Desc_Old'].unique()) + list(session_state.unmatched_data_filtered.loc[session_state.unmatched_data_filtered['PT_PDB_Commercial_Version_Flag'] == 1, 'PT_PDB_Commercial_Version_Desc_Old'].unique()) # session_state.gama_morta_per_model = matched_data_filtered['PT_PDB_Commercial_Version_Desc_Old'].unique() session_state.gama_morta_per_model = list(matched_data_filtered.loc[matched_data_filtered['PT_PDB_Commercial_Version_Flag'] == -1, 'PT_PDB_Commercial_Version_Desc_Old'].unique()) + list(session_state.unmatched_data_filtered.loc[session_state.unmatched_data_filtered['PT_PDB_Commercial_Version_Flag'] == -1, 'PT_PDB_Commercial_Version_Desc_Old'].unique()) unmatched_gamas = list(session_state.unmatched_data_filtered['PT_PDB_Commercial_Version_Desc_Old'].unique()) sel_gama = st.selectbox('Por favor escolha uma Gama:', ['-'] + list(session_state.unmatched_data_filtered['PT_PDB_Commercial_Version_Desc_Old'].unique()), index=0, key=session_state.run_id) if sel_gama != '-': sel_gama_flag = session_state.unmatched_data_filtered.loc[session_state.unmatched_data_filtered['PT_PDB_Commercial_Version_Desc_Old'] == sel_gama, 'PT_PDB_Commercial_Version_Flag'].values[0] session_state.df_sim = calculate_cosine_similarity(sel_gama, sel_gama_flag, session_state.gama_viva_per_model, session_state.gama_morta_per_model, unmatched_gamas) # print(matched_data_filtered[matched_data_filtered['PT_PDB_Commercial_Version_Flag'] == 1]) # print(session_state.unmatched_data_filtered[session_state.unmatched_data_filtered['PT_PDB_Commercial_Version_Flag'] == -1]) if session_state.df_sim.shape[0]: suggestions = session_state.df_sim[['PT_PDB_Commercial_Version_Desc_Old', 'similarity_cosine']].sort_values(by=['similarity_cosine'], ascending=False).head(5).reset_index() else: suggestions = pd.DataFrame() if sel_gama_flag == -1: if session_state.df_sim.shape[0]: st.table(suggestions[['PT_PDB_Commercial_Version_Desc_Old', 'similarity_cosine']].rename(index=str, columns={'PT_PDB_Commercial_Version_Desc_Old': 'Gama Viva', 'similarity_cosine': 'Grau de Semelhança'})) else: st.write('Sem Sugestões de Correspondência.') sel_gama_match = st.selectbox('Por favor escolha a correspondente Gama:', ['-', 's/ correspondência'] + list([x for x in session_state.gama_viva_per_model if x not in [' ', '']]), index=0, key=session_state.run_id) elif sel_gama_flag == 1: if session_state.df_sim.shape[0]: st.table(suggestions[['PT_PDB_Commercial_Version_Desc_Old', 'similarity_cosine']].rename(index=str, columns={'PT_PDB_Commercial_Version_Desc_Old': 'Gama Morta', 'similarity_cosine': 'Grau de Semelhança'})) else: st.write('Sem Sugestões de Correspondência.') sel_gama_match = st.selectbox('Por favor escolha a correspondente Gama:', ['-', 's/ correspondência'] + list([x for x in session_state.gama_morta_per_model if x not in [' ', '']]), index=0, key=session_state.run_id) if sel_gama_match != '-': if st.button('Validar') or session_state.validate_button_pressed == 1: session_state.validate_button_pressed = 1 if sel_gama_flag == -1: if sel_gama_match == 's/ correspondência': st.write('A gama morta: \n{} não possui correspondência.'.format(sel_gama)) else: st.write('A gama morta: \n{} corresponde à gama viva \n{}'.format(sel_gama, sel_gama_match)) else: if sel_gama_match == 's/ correspondência': st.write('A gama viva: \n{} não possui correspondência.'.format(sel_gama)) else: st.write('A gama viva: \n{} corresponde à gama morta \n{}'.format(sel_gama, sel_gama_match)) save_function(sel_gama, sel_gama_match, sel_brand, sel_model) session_state.validate_button_pressed = 0 session_state.run_id += 1 time.sleep(0.1) raise RerunException(RerunData()) elif sel_goal == 'Gamas Mortas Correspondidas': session_state.validate_button_pressed = 0 sel_brand = st.sidebar.selectbox('Marca:', ['-'] + list(matched_data['PT_PDB_Franchise_Desc'].unique()), index=0) if sel_brand != '-': matched_data = matched_data.loc[matched_data['PT_PDB_Franchise_Desc'] == sel_brand.upper(), :] unique_models = [x for x in list(matched_data['PT_PDB_Model_Desc'].unique()) if x not in ['H-1', 'H-1 3 lugares', 'H-1 6 lugares', 'H350', 'i20 Coupe', 'i20 VAN']] sel_model = st.sidebar.selectbox('Modelo', ['-'] + unique_models, index=0) # sel_confidence_threshold = st.sidebar.slider('Grau de Semelhança', min_value=0.0, max_value=1.0, value=0.8, step=0.01) if sel_model != '-': if sel_brand != session_state.sel_brand or sel_model != session_state.sel_model: # session_state.sel_brand = sel_brand # session_state.sel_model = sel_model matched_data_filtered = filter_data(data, [sel_model, sel_brand], ['PT_PDB_Model_Desc', 'PT_PDB_Franchise_Desc']) session_state.gama_viva_per_model = matched_data_filtered.loc[matched_data_filtered['PT_PDB_Commercial_Version_Flag'] == 1, 'PT_PDB_Commercial_Version_Desc_Old'].unique() st.write('Existem as seguintes gamas mortas correspondidas para a marca {} e modelo {}:'.format(sel_brand, sel_model)) row_even_color = 'lightgrey' row_odd_color = 'white' if matched_data_filtered.shape[0]: matched_data_temp = matched_data_filtered.loc[(matched_data_filtered['PT_PDB_Commercial_Version_Flag'] == -1) & (matched_data_filtered['Classification_Flag'] == 1)] st.subheader('Correspondências:') fig = go.Figure(data=[go.Table( columnwidth=[500, 500], header=dict( values=[['Gama Morta'], ['Gama Viva']], align=['center', 'center'], ), cells=dict( values=[matched_data_temp['PT_PDB_Commercial_Version_Desc_Old'], matched_data_temp['PT_PDB_Commercial_Version_Desc_New']], align=['center', 'center'], fill_color=[[row_odd_color, row_even_color] * matched_data_temp.shape[0]], ) ) ]) fig.update_layout(width=1100) st.write(fig) sel_gama = st.selectbox('Por favor escolha uma Gama Morta:', ['-'] + list(matched_data_temp['PT_PDB_Commercial_Version_Desc_Old'].unique()), index=0, key=session_state.run_id) if sel_gama != '-': matched_sel_gama = matched_data_filtered.loc[matched_data_filtered['PT_PDB_Commercial_Version_Desc_Old'] == sel_gama, :]['PT_PDB_Commercial_Version_Desc_New'].values[0] if len(matched_sel_gama) > 1: st.write('A Gama Viva correspondente é: {}. Se desejar alterar, escolha entre as seguintes:'.format(matched_sel_gama)) else: st.write('Para escolher uma nova correspondência, escolha entre as seguintes:') session_state.df_sim = calculate_cosine_similarity(sel_gama, -1, session_state.gama_viva_per_model, []) if session_state.df_sim.shape[0]: suggestions = session_state.df_sim[['PT_PDB_Commercial_Version_Desc_Old', 'similarity_cosine']].sort_values(by=['similarity_cosine'], ascending=False).head(5).reset_index() st.write('Sugestões:') st.table(suggestions[['PT_PDB_Commercial_Version_Desc_Old', 'similarity_cosine']].rename(index=str, columns={'PT_PDB_Commercial_Version_Desc_Old': 'Gama', 'similarity_cosine': 'Grau de Semelhança'})) else: st.write('Sem Sugestões de Correspondência.') sel_gama_match = st.selectbox('Por favor escolha a correspondente Gama:', ['-', 's/ correspondência'] + list([x for x in session_state.gama_viva_per_model if x not in [' ', '']]), index=0, key=session_state.run_id) if sel_gama_match != '-': if st.button('Validar') or session_state.validate_button_pressed == 1: session_state.validate_button_pressed = 1 st.write('A gama morta: \n{} corresponde à gama \n{}'.format(sel_gama, sel_gama_match)) save_function(sel_gama, sel_gama_match, sel_brand, sel_model) session_state.validate_button_pressed = 0 session_state.run_id += 1 time.sleep(0.1) raise RerunException(RerunData()) elif sel_goal == 'Gamas Vivas Correspondidas': session_state.validate_button_pressed = 0 sel_brand = st.sidebar.selectbox('Marca:', ['-'] + list(matched_data['PT_PDB_Franchise_Desc'].unique()), index=0) if sel_brand != '-': matched_data = matched_data.loc[matched_data['PT_PDB_Franchise_Desc'] == sel_brand.upper(), :] unique_models = [x for x in list(matched_data['PT_PDB_Model_Desc'].unique()) if x not in ['H-1', 'H-1 3 lugares', 'H-1 6 lugares', 'H350', 'i20 Coupe', 'i20 VAN']] sel_model = st.sidebar.selectbox('Modelo', ['-'] + unique_models, index=0) if sel_model != '-': if sel_brand != session_state.sel_brand or sel_model != session_state.sel_model: # session_state.sel_brand = sel_brand # session_state.sel_model = sel_model matched_data_filtered = filter_data(data, [sel_model, sel_brand], ['PT_PDB_Model_Desc', 'PT_PDB_Franchise_Desc']) session_state.gama_morta_per_model = matched_data_filtered.loc[matched_data_filtered['PT_PDB_Commercial_Version_Flag'] == -1, 'PT_PDB_Commercial_Version_Desc_Old'].unique() st.write('Existem as seguintes gamas vivas correspondidas para a marca {} e modelo {}:'.format(sel_brand, sel_model)) row_even_color = 'lightgrey' row_odd_color = 'white' if matched_data_filtered.shape[0]: matched_data_temp = matched_data_filtered.loc[(matched_data_filtered['PT_PDB_Commercial_Version_Flag'] == 1) & (matched_data_filtered['Classification_Flag'] == 1)] st.subheader('Correspondências:') fig = go.Figure(data=[go.Table( columnwidth=[500, 500], header=dict( values=[['Gama Viva'], ['Gama Correspondente']], align=['center', 'center'], ), cells=dict( values=[matched_data_temp['PT_PDB_Commercial_Version_Desc_Old'], matched_data_temp['PT_PDB_Commercial_Version_Desc_New']], align=['center', 'center'], fill_color=[[row_odd_color, row_even_color] * matched_data_temp.shape[0]], ) ) ]) fig.update_layout(width=1100) st.write(fig) sel_gama = st.selectbox('Por favor escolha uma Gama Viva:', ['-'] + list(matched_data_temp['PT_PDB_Commercial_Version_Desc_Old'].unique()), index=0, key=session_state.run_id) if sel_gama != '-': matched_sel_gama = matched_data_filtered.loc[matched_data_filtered['PT_PDB_Commercial_Version_Desc_Old'] == sel_gama, :]['PT_PDB_Commercial_Version_Desc_New'].values[0] if len(matched_sel_gama) > 1: st.write('A Gama morta correspondente é: {}. Se desejar alterar, escolha entre as seguintes:'.format(matched_sel_gama)) else: st.write('Para escolher uma nova correspondência, escolha entre as seguintes:') session_state.df_sim = calculate_cosine_similarity(sel_gama, -1, session_state.gama_morta_per_model, []) if session_state.df_sim.shape[0]: suggestions = session_state.df_sim[['PT_PDB_Commercial_Version_Desc_Old', 'similarity_cosine']].sort_values(by=['similarity_cosine'], ascending=False).head(5).reset_index() st.write('Sugestões:') st.table(suggestions[['PT_PDB_Commercial_Version_Desc_Old', 'similarity_cosine']].rename(index=str, columns={'PT_PDB_Commercial_Version_Desc_Old': 'Gama', 'similarity_cosine': 'Grau de Semelhança'})) else: st.write('Sem Sugestões de Correspondência.') sel_gama_match = st.selectbox('Por favor escolha a correspondente Gama:', ['-', 's/ correspondência'] + list([x for x in session_state.gama_morta_per_model if x not in [' ', '']]), index=0, key=session_state.run_id) if sel_gama_match != '-': if st.button('Validar') or session_state.validate_button_pressed == 1: session_state.validate_button_pressed = 1 st.write('A gama viva: \n{} corresponde à gama \n{}'.format(sel_gama, sel_gama_match)) save_function(sel_gama, sel_gama_match, sel_brand, sel_model) session_state.validate_button_pressed = 0 session_state.run_id += 1 time.sleep(0.1) raise RerunException(RerunData()) return
def enqueue_rerun(self, argv=None, widget_states=None, query_string=""): self.script_request_queue.enqueue( ScriptRequest.RERUN, RerunData(widget_states=widget_states, query_string=query_string), )
def main(): manual_classified_requests_df = get_data_non_cached(options_file, options_file.DSN_SRV3_PRD, options_file.sql_info['database_source'], options_file.sql_info['aux_table'], columns='*') auto_classified_requests_df = get_data(options_file, options_file.DSN_SRV3_PRD, options_file.sql_info['database_source'], options_file.sql_info['final_table'], columns='*', query_filters={'Classification_Flag': 1}) current_classes = get_data(options_file, options_file.DSN_MLG_PRD, options_file.sql_info['database_final'], options_file.sql_info['keywords_table'][0], columns=['Keyword_Group']) auto_classified_requests_df = auto_classified_requests_df.sort_values(by='Open_Date', ascending=False) # sel_page = st.sidebar.radio('', ['Classificações Manuais', 'Classificações via Modelo'], index=0) # if sel_page == 'Classificações Manuais': st.sidebar.text('Histórico de classificações:') last_history = manual_classified_requests_df[['Request_Num', 'Label', 'Date']].rename(columns=column_translate_dict).tail(5) st.sidebar.table(last_history) sel_prediction = st.sidebar.selectbox('Escolher Classificação via modelo:', ['-'] + [x for x in auto_classified_requests_df['Label'].unique()]) if sel_prediction != '-': filtered_data = filter_data(auto_classified_requests_df, [sel_prediction], ['Label'], [None]) else: filtered_data = auto_classified_requests_df st.sidebar.text('Para atualizar a tabela final no DW:') if st.sidebar.button('Atualizar DataWarehouse') or session_state.update_final_table_button_pressed_flag == 1: session_state.update_final_table_button_pressed_flag = 1 if not manual_classified_requests_df.shape[0]: st.sidebar.text('ERRO - Não existem atualmente pedidos') st.sidebar.text('classificados manualmente.') session_state.update_final_table_button_pressed_flag = 0 else: update_dw(update_query.format(options_file.sql_info['initial_table_facts'], options_file.sql_info['initial_table_facts'], options_file.sql_info['aux_table']), options_file, options_file.DSN_SRV3_PRD, options_file.sql_info['database_source']) session_state.update_final_table_button_pressed_flag = 0 if manual_classified_requests_df.shape[0]: manual_classified_reqs = manual_classified_requests_df['Request_Num'].unique() filtered_data = filtered_data.loc[~filtered_data['Request_Num'].isin(manual_classified_reqs), :] fig = go.Figure(data=[go.Table( columnwidth=[120, 900, 120, 120], header=dict( values=[['Nº Pedido'], ['Descrição'], ['Data Abertura'], ['Classificação via Modelo']], align=['center', 'center', 'center', 'center'], ), cells=dict( values=[filtered_data['Request_Num'], filtered_data['Description'], filtered_data['Open_Date'], filtered_data['Label']], align=['center', 'right', 'center', 'center'], ) ) ]) fig.update_layout(width=1600) st.write(fig) st.write('Nº Pedidos: {}'.format(filtered_data['Request_Num'].nunique())) sel_req = st.multiselect('Por favor escolha um Pedido:', filtered_data['Request_Num'].unique(), key=session_state.run_id) if len(sel_req) == 1: description = filtered_data.loc[filtered_data['Request_Num'] == sel_req[0]]['Description'].values[0] st.write('Descrição do pedido {}:'.format(sel_req[0])) st.write('"" {} ""'.format(description)) # ToDO: add markdown configuration like bold or italic sel_label = st.multiselect('Por favor escolha uma Categoria para o pedido {}:'.format(sel_req[0]), current_classes['Keyword_Group'].unique()) if len(sel_label) == 1: previous_label = manual_classified_requests_df.loc[manual_classified_requests_df['Request_Num'] == sel_req[0], 'Label'].values if st.button('Gravar Classificação') or session_state.save_button_pressed_flag == 1: session_state.save_button_pressed_flag = 1 if previous_label or session_state.overwrite_button_pressed_flag == 1: st.write('O pedido {} já foi previamente classificado como {}'.format(sel_req[0], previous_label[0])) # ToDO: add markdown configuration like bold or italic st.write('Pretende substituir pela classe atual?') if st.button('Sim'): solution_saving(options_file, options_file.DSN_SRV3_PRD, options_file.sql_info['database_source'], options_file.sql_info['aux_table'], sel_req[0], sel_label[0]) session_state.overwrite_button_pressed_flag, session_state.save_button_pressed_flag = 0, 0 session_state.run_id += 1 time.sleep(0.1) raise RerunException(RerunData()) else: solution_saving(options_file, options_file.DSN_SRV3_PRD, options_file.sql_info['database_source'], options_file.sql_info['aux_table'], sel_req[0], sel_label[0]) session_state.overwrite_button_pressed_flag, session_state.save_button_pressed_flag = 0, 0 session_state.run_id += 1 time.sleep(0.1) raise RerunException(RerunData()) elif len(sel_label) > 1: st.error('Por favor escolha apenas uma classe.') elif len(sel_req) > 1: st.error('Por favor escolha um pedido para classificar de cada vez.')
def main(): data_v2 = get_data_v2(options_file, options_file.DSN_MLG_PRD, options_file.sql_info['database_final'], options_file.sql_info['new_score_streamlit_view'], model_flag=1) all_brands_sales_plan = get_data_v2( options_file, options_file.DSN_SRV3_PRD, options_file.sql_info['database_source'], options_file.sql_info['sales_plan_aux']) live_ocn_df = get_data_v2(options_file, options_file.DSN_MLG_PRD, options_file.sql_info['database_final'], options_file.sql_info['current_live_ocn_table'], model_flag=1) end_month_index, current_year = period_calculation() dw_last_updated_date = data_v2['Record_Date'].max() placeholder_dw_date.markdown( "<p style='text-align: right;'>Última Atualização DW - {}</p>".format( dw_last_updated_date), unsafe_allow_html=True) data_v2 = col_normalization(data_v2.copy(), cols_to_normalize, reverse_normalization_cols) max_number_of_cars_sold = max(data_v2['Sum_Qty_CHS']) sel_brand = st.sidebar.selectbox( 'Marca:', ['-'] + [x for x in options_file.nlr_code_desc.keys()], index=0, key=session_state.run_id) if '-' not in sel_brand: co2_nedc, co2_wltp, total_sales = co2_processing( all_brands_sales_plan.loc[ all_brands_sales_plan['NLR_Code'] == str(options_file.nlr_code_desc[sel_brand]), :].copy(), end_month_index, current_year) co2_nedc_before_order = co2_nedc / total_sales co2_wltp_before_order = co2_wltp / total_sales st.write( 'Situação Atual de Co2 (NEDC/WLTP): {:.2f}/{:.2f} gCo2/km'.format( co2_nedc_before_order, co2_wltp_before_order)) if end_month_index == 1: sel_period_string = '{} de {}'.format('Jan', current_year) else: sel_period_string = '{} a {} de {}'.format( total_months_list[0], total_months_list[end_month_index - 1], current_year) st.write('Plano de Vendas Total, {}: {} viaturas'.format( sel_period_string, int(total_sales))) placeholder_sales_plan_single_model = st.empty() data_models_v2 = data_v2.loc[ data_v2['NLR_Code'] == str(options_file.nlr_code_desc[sel_brand]), 'PT_PDB_Model_Desc'].unique() sel_model = st.sidebar.selectbox('Modelo:', ['-'] + list(sorted(data_models_v2)), index=0) sales_plan_last_updated_date = all_brands_sales_plan.loc[ all_brands_sales_plan['NLR_Code'] == str(options_file.nlr_code_desc[sel_brand]), 'Record_Date'].max() proposals_last_updated_date = run_single_query( options_file.DSN_SRV3_PRD, options_file.sql_info['database_source'], options_file, options_file.proposals_max_date_query.format( options_file.nlr_code_desc[sel_brand])).values[0][0] margins_last_update_date = run_single_query( options_file.DSN_SRV3_PRD, options_file.sql_info['database_source'], options_file, options_file.margins_max_date_query.format( options_file.nlr_code_desc[sel_brand])).values[0][0] placeholder_sales_plan_date.markdown( "<p style='text-align: right;'>Última Atualização Plano de Vendas - {}</p>" .format(sales_plan_last_updated_date), unsafe_allow_html=True) placeholder_margins_date.markdown( "<p style='text-align: right;'>Última Atualização Margens HP - {}</p>" .format(margins_last_update_date), unsafe_allow_html=True) if sel_brand == 'Hyundai': placeholder_proposal_date.markdown( "<p style='text-align: right;'>Última Atualização Propostas HPK - {}</p>" .format(proposals_last_updated_date), unsafe_allow_html=True) elif sel_brand == 'Honda': placeholder_proposal_date.markdown( "<p style='text-align: right;'>Última Atualização Propostas - {}</p>" .format(proposals_last_updated_date), unsafe_allow_html=True) else: raise ValueError('Unknown Selected Brand - {}'.format(sel_brand)) else: sel_model = '' st.sidebar.title('Opções:') sel_order_size = st.sidebar.number_input( 'Por favor escolha o número de viaturas a encomendar:', 1, 1000, value=150) sel_min_number_of_configuration = st.sidebar.number_input( 'Por favor escolha o número de configurações:', 1, 100, value=min_number_of_configuration) placeholder_value = st.sidebar.empty() sel_min_sold_cars = st.sidebar.number_input( 'Por favor escolha um valor mínimo de viaturas vendidas por configuração (valor máximo é de {:.0f}):' .format(max_number_of_cars_sold), 1, int(max_number_of_cars_sold), value=5) st.sidebar.title('Pesos:') session_state.sel_daysinstock_score_weight = st.sidebar.number_input( 'Por favor escolha um peso para o critério de Dias em Stock: (default={:.0f}%)' .format(score_weights['Avg_DaysInStock_Global_normalized'] * 100), 0, 100, value=int(score_weights['Avg_DaysInStock_Global_normalized'] * 100), key=session_state.run_id_scores) session_state.sel_margin_score_weight = st.sidebar.number_input( 'Por favor escolha um peso para o critério de Margem: (default={:.0f}%)' .format(score_weights['TotalGrossMarginPerc_normalized'] * 100), 0, 100, value=int(score_weights['TotalGrossMarginPerc_normalized'] * 100), key=session_state.run_id_scores) session_state.sel_margin_ratio_score_weight = st.sidebar.number_input( 'Por favor escolha um peso para o critério de Rácio de Margem: (default={:.0f}%)' .format(score_weights['MarginRatio_normalized'] * 100), 0, 100, value=int(score_weights['MarginRatio_normalized'] * 100), key=session_state.run_id_scores) session_state.sel_qty_sold_score_weight = st.sidebar.number_input( 'Por favor escolha um peso para o critério de Volume de Vendas: (default={:.0f}%)' .format(score_weights['Sum_Qty_CHS_normalized'] * 100), 0, 100, value=int(score_weights['Sum_Qty_CHS_normalized'] * 100), key=session_state.run_id_scores) session_state.sel_proposals_score_weight = st.sidebar.number_input( 'Por favor escolha um peso para o critério de Propostas: (default={:.0f}%)' .format(score_weights['Proposals_VDC_normalized'] * 100), 0, 100, value=int(score_weights['Proposals_VDC_normalized'] * 100), key=session_state.run_id_scores) session_state.sel_oc_stock_diff_score_weight = st.sidebar.number_input( 'Por favor escolha um peso para o critério de O.C. vs Stock: (default={:.0f}%)' .format(score_weights['Stock_OC_Diff_normalized'] * 100), 0, 100, value=int(score_weights['Stock_OC_Diff_normalized'] * 100), key=session_state.run_id_scores) session_state.sel_co2_nedc_score_weight = st.sidebar.number_input( 'Por favor escolha um peso para o critério de Co2 (NEDC): (default={:.0f}%)' .format(score_weights['NEDC_normalized'] * 100), 0, 100, value=int(score_weights['NEDC_normalized'] * 100), key=session_state.run_id_scores) if '-' not in sel_brand: if options_file.nlr_code_desc[sel_brand] == 702: session_state.sel_configurator_count_score_weight = st.sidebar.number_input( 'Por favor escolha um peso para o critério de Configurador: (default={:.0f}%)' .format(score_weights['Configurator_Count_normalized'] * 100), 0, 100, value=int(score_weights['Configurator_Count_normalized'] * 100), key=session_state.run_id_scores) elif options_file.nlr_code_desc[sel_brand] == 706: session_state.sel_configurator_count_score_weight = 0 weights_sum = session_state.sel_daysinstock_score_weight + session_state.sel_margin_score_weight + session_state.sel_margin_ratio_score_weight + session_state.sel_qty_sold_score_weight + session_state.sel_proposals_score_weight + session_state.sel_oc_stock_diff_score_weight + session_state.sel_co2_nedc_score_weight + session_state.sel_configurator_count_score_weight if weights_sum != 100: st.sidebar.error( 'Alerta: Soma dos pesos é atualmente de {}%. Por favor validar e corrigir pesos de acordo.' .format(weights_sum)) if st.sidebar.button('Reset Pesos'): session_state.sel_daysinstock_score_weight = score_weights[ 'Avg_DaysInStock_Global_normalized'] * 100 session_state.sel_margin_score_weight = score_weights[ 'TotalGrossMarginPerc_normalized'] * 100 session_state.sel_margin_ratio_score_weight = score_weights[ 'MarginRatio_normalized'] * 100 session_state.sel_qty_sold_score_weight = score_weights[ 'Sum_Qty_CHS_normalized'] * 100 session_state.sel_proposals_score_weight = score_weights[ 'Proposals_VDC_normalized'] * 100 session_state.sel_oc_stock_diff_score_weight = score_weights[ 'Stock_OC_Diff_normalized'] * 100 session_state.sel_co2_nedc_score_weight = score_weights[ 'NEDC_normalized'] * 100 if options_file.nlr_code_desc[sel_brand] == 702: session_state.sel_configurator_count_score_weight = score_weights[ 'Configurator_Count_normalized'] * 100 elif options_file.nlr_code_desc[sel_brand] == 706: session_state.sel_configurator_count_score_weight = 0 session_state.run_id_scores += 1 raise RerunException(RerunData()) data_v2['Score'] = data_v2.apply( score_calculation, args=(session_state.sel_daysinstock_score_weight / 100, session_state.sel_margin_score_weight / 100, session_state.sel_margin_ratio_score_weight / 100, session_state.sel_qty_sold_score_weight / 100, session_state.sel_proposals_score_weight / 100, session_state.sel_oc_stock_diff_score_weight / 100, session_state.sel_co2_nedc_score_weight / 100, session_state.sel_configurator_count_score_weight / 100), axis=1) if sel_model != session_state.model: session_state.model = sel_model session_state.overwrite_button_pressed, session_state.save_button_pressed_flag = 0, 0 if '-' not in [sel_model] and '-' not in [sel_brand]: sales_plan_sel_model_sales = run_single_query( options_file.DSN_SRV3_PRD, options_file.sql_info['database_source'], options_file, options_file.sales_plan_current_sales_single_model.format( end_month_index - 1, sel_model)).values[0][0] placeholder_sales_plan_single_model.write( 'Plano de Vendas para {}, {}: {} viaturas'.format( sel_model, sel_period_string, int(sales_plan_sel_model_sales))) # data_filtered = filter_data(data, [sel_model, sel_min_sold_cars], ['PT_PDB_Model_Desc', 'Quantity_Sold']) data_filtered_v2 = filter_data_v2(data_v2, [sel_model, sel_min_sold_cars], ['PT_PDB_Model_Desc', 'Sum_Qty_CHS']) if not data_filtered_v2.shape[0]: st.write( 'Não foram encontrados registos para as presentes escolhas - Por favor altere o modelo/cliente/valor mínimo de viaturas por configuração.' ) return sel_configurations_v2 = quantity_processing_v2( data_filtered_v2.head(sel_min_number_of_configuration).copy( deep=True), sel_order_size) if sel_configurations_v2.shape[0]: sel_configurations_v2.rename( index=str, columns={ 'Quantity': 'Sug.Encomenda', 'Sum_Qty_CHS': '#Veículos Vendidos' }, inplace=True ) # ToDo: For some reason this column in particular is not changing its name by way of the renaming argument in the previous st.write. This is a temporary solution st.markdown( "<h3 style='text-align: left;'>Sugestão de Encomenda - Score v2:</h3>", unsafe_allow_html=True) if options_file.nlr_code_desc[sel_brand] == 702: st.write( '', sel_configurations_v2[['Sug.Encomenda'] + [ x for x in configuration_parameters if x not in 'PT_PDB_Model_Desc' ] + temp_cols + ['Score']].rename( columns=options_file.column_translate_dict). reset_index(drop=True).style.apply( highlight_cols, col_dict=options_file.col_color_dict).format( options_file.col_decimals_place_dict)) sel_configurations_v2.rename( index=str, columns={'Sug.Encomenda': 'Quantity'}, inplace=True ) # ToDo: For some reason this column in particular is not changing its name by way of the renaming argument in the previous st.write. This is a temporary solution elif options_file.nlr_code_desc[sel_brand] == 706: st.write( '', sel_configurations_v2[['Sug.Encomenda'] + [ x for x in configuration_parameters if x not in 'PT_PDB_Model_Desc' ] + [x for x in temp_cols if not x.startswith('Config') ] + ['Score']].rename( columns=options_file.column_translate_dict ).reset_index(drop=True).style.apply( highlight_cols, col_dict=options_file.col_color_dict).format( options_file.col_decimals_place_dict)) sel_configurations_v2.rename( index=str, columns={'Sug.Encomenda': 'Quantity'}, inplace=True ) # ToDo: For some reason this column in particular is not changing its name by way of the renaming argument in the previous st.write. This is a temporary solution if sel_min_number_of_configuration > sel_configurations_v2.shape[0]: placeholder_value.error( "Alerta: Número mínimo de configurações é superior ao número de configurações disponíveis para este modelo ({})." .format(sel_configurations_v2.shape[0])) total_sales_after_order = total_sales + sel_configurations_v2[ 'Quantity'].sum() sel_configurations_v2['nedc_after_order'] = sel_configurations_v2[ 'Quantity'] * sel_configurations_v2['NEDC'] sel_configurations_v2['wltp_after_order'] = sel_configurations_v2[ 'Quantity'] * sel_configurations_v2['WLTP'] co2_nedc_after_order = co2_nedc + sel_configurations_v2[ 'nedc_after_order'].sum() co2_wltp_after_order = co2_wltp + sel_configurations_v2[ 'wltp_after_order'].sum() co2_nedc_per_vehicle_after_order = co2_nedc_after_order / total_sales_after_order co2_wltp_per_vehicle_after_order = co2_wltp_after_order / total_sales_after_order co2_nedc_per_vehicle_evolution = co2_nedc_per_vehicle_after_order - co2_nedc_before_order if co2_nedc_per_vehicle_evolution > 0: st.markdown( "Situação Co2 (NEDC) após esta encomenda: {:.2f}(<span style='color:red'>+{:.2f}</span>) gCo2/km" .format(co2_nedc_per_vehicle_after_order, co2_nedc_per_vehicle_evolution), unsafe_allow_html=True) elif co2_nedc_per_vehicle_evolution < 0: st.markdown( "Situação Co2 (NEDC) após esta encomenda: {:.2f}(<span style='color:green'>{:.2f}</span>) gCo2/km" .format(co2_nedc_per_vehicle_after_order, co2_nedc_per_vehicle_evolution), unsafe_allow_html=True) else: st.markdown( "Situação Co2 (NEDC) sem alterações após esta encomenda.") co2_wltp_per_vehicle_evolution = co2_wltp_per_vehicle_after_order - co2_wltp_before_order if co2_wltp_per_vehicle_evolution > 0: st.markdown( "Situação Co2 (WLTP) após esta encomenda: {:.2f}(<span style='color:red'>+{:.2f}</span>) gCo2/km" .format(co2_wltp_per_vehicle_after_order, co2_wltp_per_vehicle_evolution), unsafe_allow_html=True) elif co2_wltp_per_vehicle_evolution < 0: st.markdown( "Situação Co2 (WLTP) após esta encomenda: {:.2f}(<span style='color:green'>{:.2f}</span>) gCo2/km" .format(co2_wltp_per_vehicle_after_order, co2_wltp_per_vehicle_evolution), unsafe_allow_html=True) else: st.markdown( "Situação Co2 (WLTP) sem alterações após esta encomenda.") df_to_export = file_export_preparation( sel_configurations_v2[[ 'Quantity', 'PT_PDB_Model_Desc', 'PT_PDB_Engine_Desc', 'PT_PDB_Transmission_Type_Desc', 'PT_PDB_Version_Desc', 'PT_PDB_Exterior_Color_Desc', 'PT_PDB_Interior_Color_Desc' ]].reset_index(drop=True), live_ocn_df, sel_brand) file_export( df_to_export.rename( columns=options_file.column_translate_dict), 'Sugestão_Encomenda_{}_{}_'.format(sel_brand, sel_model)) sel_configurations_v2['Configuration_Concat'] = sel_configurations_v2[ 'PT_PDB_Model_Desc'] + ', ' + sel_configurations_v2[ 'PT_PDB_Engine_Desc'] + ', ' + sel_configurations_v2[ 'PT_PDB_Transmission_Type_Desc'] + ', ' + sel_configurations_v2[ 'PT_PDB_Version_Desc'] + ', ' + sel_configurations_v2[ 'PT_PDB_Exterior_Color_Desc'] + ', ' + sel_configurations_v2[ 'PT_PDB_Interior_Color_Desc'] st.markdown( "<h3 style='text-align: left;'>Configuração a explorar:</h3>", unsafe_allow_html=True) sel_config = st.selectbox('', ['-'] + [ x for x in sel_configurations_v2['Configuration_Concat'].unique() ], index=0) if sel_config != '-': validation_dfs_v2 = get_validation_info( sel_configurations_v2, sel_config) validation_dfs_titles = [ 'Vendas ({}):', 'Propostas ({}, para os últimos 3 meses):', 'Stock ({}):', 'Plano de Vendas, passo 1:', 'Plano de Vendas, passo 2 - selecionando os máximos de valores de quantidade por período:', 'Plano de Vendas, passo 3 - aplicando a seguinte fórmula: *Objetivo de Cobertura de Stock no mês N = 2,5 \* média das vendas de (N, N+1, N+2, N+3 e N+4)*:' ] validation_queries_display( str_title=validation_dfs_titles[0], int_count=int(validation_dfs_v2[0]['Quantity_CHS'].sum()), df_data=validation_dfs_v2[0][[ x for x in list(validation_dfs_v2[0]) if x != 'Last_Modified_Date' ]]) validation_queries_display( str_title=validation_dfs_titles[1], int_count=validation_dfs_v2[1].shape[0], df_data=validation_dfs_v2[1][[ x for x in list(validation_dfs_v2[1]) if x != 'Last_Modified_Date' ]]) validation_queries_display( str_title=validation_dfs_titles[3], df_data=validation_dfs_v2[3][[ x for x in list(validation_dfs_v2[3]) if x != 'Last_Modified_Date' ]]) validation_queries_display( str_title=validation_dfs_titles[4], df_data=validation_dfs_v2[4][[ x for x in list(validation_dfs_v2[4]) if x != 'Last_Modified_Date' ]]) validation_queries_display( str_title=validation_dfs_titles[5], df_data=validation_dfs_v2[5][[ x for x in list(validation_dfs_v2[5]) if x != 'Last_Modified_Date' ]]) validation_queries_display( str_title=validation_dfs_titles[2], int_count=validation_dfs_v2[2].shape[0], df_data=validation_dfs_v2[2][[ x for x in list(validation_dfs_v2[2]) if x != 'Last_Modified_Date' ]]) else: return else: st.markdown( "<p style='text-align: center;'>Por favor escolha uma marca e modelo para sugerir a respetiva encomenda.</p>", unsafe_allow_html=True)
def main(): df_product_group = get_data_product_group_sql(options_file.others_families_dict, options_file) cm_family_lvl_1 = get_data_sql(options_file, options_file.sql_info['database_final'], options_file.sql_info['matrix_lvl_1']) cm_family_lvl_2 = get_data_sql(options_file, options_file.sql_info['database_final'], options_file.sql_info['matrix_lvl_2']) cm_family_dict_lvl_1 = cm_replacements(cm_family_lvl_1) cm_family_dict_lvl_2 = cm_replacements(cm_family_lvl_2) family_dict_sorted = family_dict_sorting(cm_family_dict_lvl_1, cm_family_dict_lvl_2) st.sidebar.title('Objetivo:') sel_page = st.sidebar.radio('', ['Análise de Classificações', 'Correções às Famílias Atuais', 'Exportação de Classificações'], index=0) if sel_page == 'Correções às Famílias Atuais': data_original = get_dataset_sql(options_file.others_families_dict, options_file, options_file.classified_app_query) data = product_group_description(data_original, df_product_group) lower_performance_families = family_dict_sorted.keys() lower_performance_families_values = [x[0] * 100 for x in family_dict_sorted.values()] df_current_cm = pd.DataFrame() df_current_cm['Product_Group_DW'] = lower_performance_families df_current_cm['Percentage_Predicted'] = lower_performance_families_values df_current_cm = df_current_cm.merge(df_product_group[['Product_Group_Code', 'PT_Product_Group_Desc']], left_on='Product_Group_DW', right_on='Product_Group_Code', how='left').drop('Product_Group_Code', axis=1).rename(columns={'PT_Product_Group_Desc': 'Product_Group_DW_desc'}) df_current_cm.loc[df_current_cm['Product_Group_DW_desc'].isnull(), 'Product_Group_DW_desc'] = df_current_cm.loc[df_current_cm['Product_Group_DW_desc'].isnull(), 'Product_Group_DW'] st.sidebar.title('Famílias com pior performance:') st.sidebar.table(df_current_cm[['Product_Group_DW_desc', 'Percentage_Predicted']].rename(columns=options_file.column_translate_dict).head(15).style.format({'% Conf.': '{:.2f}'})) sel_family_desc = st.sidebar.selectbox('Por favor escolha a família de peças para alterar:', ['-'] + [x for x in df_current_cm['Product_Group_DW_desc'].unique()]) # ToDo: Maybe give more options? if sel_family_desc != '-': if session_state.sel_family_desc != sel_family_desc: session_state.sel_family_desc = sel_family_desc session_state.run_id += 1 if sel_family_desc not in options_file.others_families_dict.values(): sel_family_code = df_product_group.loc[df_product_group['PT_Product_Group_Desc'] == sel_family_desc, 'Product_Group_Code'].values[0] else: sel_family_code = sel_family_desc sim_family_code = family_dict_sorted[sel_family_code][1][1] if sim_family_code not in options_file.others_families_dict.values(): sim_family_desc = df_product_group.loc[df_product_group['Product_Group_Code'] == sim_family_code, 'PT_Product_Group_Desc'].values[0] else: sim_family_desc = sim_family_code st.write('A Família escolhida - {} - foi confundida com a família {}, em cerca de {:.2f}% das suas classificações. Estas são as palavras mais confundidas entre as duas famílias:'.format(sel_family_desc, sim_family_desc, family_dict_sorted[sel_family_code][2][0] * 100)) session_state.data_filtered_sel = filter_data(data, [sel_family_desc], ['Product_Group_DW_desc'], [None]) session_state.data_filtered_sim = filter_data(data, [sim_family_desc], ['Product_Group_DW_desc'], [None]) min_cost, max_cost, min_pvp, max_pvp = cost_and_pvp_limits() df_common_keywords = common_keywords_calculation(sel_family_desc) left_table, middle_table, right_table = st.beta_columns(3) with middle_table: st.write(df_common_keywords.head(50)) sel_text = st.text_input('Pesquisar pela(s) palavra(s):', '', key=session_state.run_id) sel_text_option = st.radio('Escolha a forma de pesquisa:', ('contains', 'starts'), format_func=radio_button_options, key=session_state.run_id) st.sidebar.title('Filtros:') sel_costs = st.sidebar.slider('Por favor escolha os valores limite de custo:', min_cost, max_cost, (min_cost, max_cost), 10.0) sel_pvps = st.sidebar.slider('Por favor escolha os valores limite de venda:', min_pvp, max_pvp, (min_pvp, max_pvp), 10.0) # sel_original_class = st.selectbox('Por favor escolha a família original a filtrar:', ['-'] + filtered_original_classes, index=0) if sel_text != '': # if sel_costs[1] != max_cost or sel_costs[0] != min_cost or sel_pvps[1] != max_pvp or sel_pvps[0] != min_pvp: session_state.data_filtered_sim = filter_data(session_state.data_filtered_sim, [sel_costs[1], sel_costs[0], sel_pvps[1], sel_pvps[0]], ['Part_Cost', 'Part_Cost', 'Part_PVP', 'Part_PVP'], ['le', 'ge', 'le', 'ge']) session_state.data_filtered_sel = filter_data(session_state.data_filtered_sel, [sel_costs[1], sel_costs[0], sel_pvps[1], sel_pvps[0]], ['Part_Cost', 'Part_Cost', 'Part_PVP', 'Part_PVP'], ['le', 'ge', 'le', 'ge']) description_filtering(sel_text_option, sel_text) if session_state.data_text_filtered_sel.shape[0]: fig = go.Figure(data=[go.Table( columnwidth=[], header=dict( values=[options_file.column_translate_dict['Part_Ref'], options_file.column_translate_dict['Part_Description'], options_file.column_translate_dict['Part_Cost'], options_file.column_translate_dict['Part_PVP'], options_file.column_translate_dict['Product_Group_DW_desc'], options_file.column_translate_dict['Classification_desc'], options_file.column_translate_dict['Classification_Prob']], align=['center', 'center', 'center', 'center'], ), cells=dict( values=[session_state.data_text_filtered_sel['Part_Ref'], session_state.data_text_filtered_sel['Part_Description'], session_state.data_text_filtered_sel['Part_Cost'].round(2), session_state.data_text_filtered_sel['Part_PVP'].round(2), session_state.data_text_filtered_sel['Product_Group_DW_desc'], session_state.data_text_filtered_sel['Classification_desc'], session_state.data_text_filtered_sel['Classification_Prob'].round(2)], align=['center', 'left', 'center', 'center'], ), )] ) fig.update_layout(width=1500, height=500, title='Família Escolhida: {} - Nº de Peças encontradas: {}'.format(sel_family_desc, session_state.data_text_filtered_sel.shape[0])) st.write(fig) sel_family_sel_overwrite = st.selectbox('Por favor escolha a família para as peças selecionadas: ', ['-'] + [x for x in df_product_group['PT_Product_Group_Desc'].unique()], key=session_state.run_id+1, format_func=lambda x: df_product_group.loc[df_product_group['PT_Product_Group_Desc'] == x, 'Product_Group_Merge'].values[0] if x != '-' else '-') if st.button('Validar alteração', key=0): if sel_family_sel_overwrite == '-': st.error('Por favor selecione uma família de peças.') else: update_family(session_state.data_text_filtered_sel.copy(), sel_family_sel_overwrite, df_product_group) save_classification_rule(df_product_group, session_state.sel_text, sel_text_option, sel_family_sel_overwrite, sel_costs[1], max_cost, sel_costs[0], min_cost, sel_pvps[1], max_pvp, sel_pvps[0], min_pvp) else: st.write(options_file.warning_message_app_dict[sel_text_option].format(sel_family_desc, session_state.sel_text)) if session_state.data_text_filtered_sim.shape[0]: fig = go.Figure(data=[go.Table( columnwidth=[], header=dict( values=[options_file.column_translate_dict['Part_Ref'], options_file.column_translate_dict['Part_Description'], options_file.column_translate_dict['Part_Cost'], options_file.column_translate_dict['Part_PVP'], options_file.column_translate_dict['Product_Group_DW_desc'], options_file.column_translate_dict['Classification_desc'], options_file.column_translate_dict['Classification_Prob']], align=['center', 'center', 'center', 'center', 'center', 'center'], ), cells=dict( values=[session_state.data_text_filtered_sim['Part_Ref'], session_state.data_text_filtered_sim['Part_Description'], session_state.data_text_filtered_sim['Part_Cost'].round(2), session_state.data_text_filtered_sim['Part_PVP'].round(2), session_state.data_text_filtered_sim['Product_Group_DW_desc'], session_state.data_text_filtered_sim['Classification_desc'], session_state.data_text_filtered_sim['Classification_Prob'].round(2)], align=['center', 'left', 'center', 'center', 'center', 'center'], ), )] ) fig.update_layout(width=1500, height=500, title='Família Semelhante: {} - Nº de Peças encontradas: {}'.format(sim_family_desc, session_state.data_text_filtered_sim.shape[0])) st.write(fig) sel_family_sim_overwrite = st.selectbox('Por favor escolha a família para as peças selecionadas: ', ['-'] + [x for x in df_product_group['PT_Product_Group_Desc'].unique()], key=session_state.run_id, format_func=lambda x: df_product_group.loc[df_product_group['PT_Product_Group_Desc'] == x, 'Product_Group_Merge'].values[0] if x != '-' else '-') if st.button('Validar alteração', key=1): if sel_family_sim_overwrite == '-': st.error('Por favor selecione uma família de peças.') else: update_family(session_state.data_text_filtered_sim.copy(), sel_family_sim_overwrite, df_product_group) save_classification_rule(df_product_group, session_state.sel_text, sel_text_option, sel_family_sim_overwrite, sel_costs[1], max_cost, sel_costs[0], min_cost, sel_pvps[1], max_pvp, sel_pvps[0], min_pvp) else: st.write(options_file.warning_message_app_dict[sel_text_option].format(sim_family_desc, session_state.sel_text)) else: st.write('Por favor escolha uma família de peças.') elif sel_page == 'Análise de Classificações': data_original = get_dataset_sql(options_file.others_families_dict, options_file, options_file.non_classified_app_query) data = product_group_description(data_original, df_product_group) st.subheader('Por favor escolha a família a explorar: ') sel_family = st.selectbox('', ['-'] + [x for x in df_product_group['PT_Product_Group_Desc'].unique()], key=session_state.run_id, format_func=lambda x: df_product_group.loc[df_product_group['PT_Product_Group_Desc'] == x, 'Product_Group_Merge'].values[0] if x != '-' else '-') sel_family_converted = family_code_convertion(sel_family, df_product_group) sel_all_refs_flag = st.sidebar.checkbox('Selecionar todas as referências.', value=True) st.subheader('Ou em alternativa, pesquise pela(s) palavra(s):') sel_text = st.text_input('', key=session_state.run_id) sel_text_option = st.sidebar.radio('Escolha a forma de pesquisa na descrição:', ('equals', 'contains', 'starts'), format_func=radio_button_options, index=0) if sel_family != '-': data_filtered = filter_data(data, [sel_family_converted], ['Product_Group_DW'], [None]) st.write('#Peças da familía {}: {}'.format(sel_family, data_filtered.shape[0])) unique_part_descs = data_filtered['Part_Description'].value_counts().reset_index() table_title = 'Descrições existentes na família {} - {} descrições:'.format(sel_family, unique_part_descs.shape[0]) fig = go.Figure(data=[go.Table( columnwidth=[], header=dict( values=['Descrição', 'Contagem'], align=['center', 'center'], ), cells=dict( values=[unique_part_descs['index'], unique_part_descs['Part_Description']], align=['left', 'center'], ), )] ) fig.update_layout(width=800, height=500, title=table_title) st.write(fig) sel_text = st.selectbox('Lista de descrições para a família selecionada:', ['-'] + [x for x in unique_part_descs['index'].values]) if sel_text not in ['', '-']: if sel_text_option == 'starts': data_df = data.loc[data['Part_Description'].str.startswith(sel_text), :] elif sel_text_option == 'contains': sel_text_regex = sel_text_regex_conversion(sel_text) data_df = data.loc[data['Part_Description'].str.contains(sel_text_regex, case=False, regex=True), :] elif sel_text_option == 'equals': data_df = filter_data(data, [sel_text], ['Part_Description'], [None]) if data_df.shape[0] > 0: table_title = 'Peças que {} - {} ({} referências):'.format(radio_button_options('table_' + sel_text_option), sel_text, data_df.shape[0]) fig = go.Figure(data=[go.Table( columnwidth=[], header=dict( values=[options_file.column_translate_dict['Part_Ref'], options_file.column_translate_dict['Part_Description'], options_file.column_translate_dict['Part_Cost'], options_file.column_translate_dict['Part_PVP'], options_file.column_translate_dict['Product_Group_DW_desc'], options_file.column_translate_dict['Classification_desc'], options_file.column_translate_dict['Classification_Prob']], align=['center', 'center', 'center', 'center'], ), cells=dict( # values=[session_state.data['Part_Ref'].head(50), session_state.data['Part_Description'].head(50), session_state.data['Part_Cost'].round(2).head(50), session_state.data['Part_PVP'].round(2).head(50), session_state.data['Product_Group_DW_desc'].head(50), session_state.data['Classification_desc'].head(50), session_state.data['Classification_Prob'].round(2).head(50)], values=[data_df['Part_Ref'], data_df['Part_Description'], data_df['Part_Cost'].round(2), data_df['Part_PVP'].round(2), data_df['Product_Group_DW_desc'], data_df['Classification_desc'], data_df['Classification_Prob'].round(2)], align=['center', 'left', 'center', 'center'], ), )] ) fig.update_layout(width=1500, height=500, title=table_title) st.write(fig) else: st.error('Não existem peças nas condições referidas. Por favor altere o(s) valor(es) do(s) filtro(s).') if not sel_all_refs_flag: sel_part_ref = st.multiselect('Por favor escolha a(s) referência(s) a alterar:', [x for x in data_df['Part_Ref'].unique()], key=session_state.run_id) data_df = filter_data(data_df, [sel_part_ref], ['Part_Ref'], ['in']) sel_family_overwrite = st.selectbox('Por favor escolha a família para as peças selecionadas: ', ['-'] + [x for x in df_product_group['PT_Product_Group_Desc'].unique()], key=session_state.run_id, format_func=lambda x: df_product_group.loc[df_product_group['PT_Product_Group_Desc'] == x, 'Product_Group_Merge'].values[0] if x != '-' else '-') if st.button('Alterar'): if not data_df.shape[0]: st.error('Por favor escolha as referências a alterar.') return if sel_family_overwrite != '-': update_family(data_df.copy(), sel_family_overwrite, df_product_group) session_state.run_id += 1 time.sleep(0.1) raise RerunException(RerunData()) elif sel_family_overwrite == '-': st.error('Por favor escolha uma família de peças.') elif sel_page == 'Exportação de Classificações': st.write('Nesta página é possível exportar uma família completa de peças, de acordo com a classificação mais recente do modelo de machine learning.') data = get_dataset_sql(options_file.others_families_dict, options_file, options_file.classified_app_query) current_date, _ = level_1_e_deployment.time_tags(format_date='%Y%m%d') available_families = df_product_group['Product_Group_Code'].unique() sel_family_desc = st.selectbox('Por favor escolha a família para as peças selecionadas: ', ['-'] + [x for x in available_families], key=session_state.run_id + 1, format_func=lambda x: df_product_group.loc[df_product_group['Product_Group_Code'] == x, 'Product_Group_Merge'].values[0] if x != '-' else '-') if sel_family_desc != '-' and int(sel_family_desc) in options_file.others_families_dict.keys(): sel_family_desc = options_file.others_families_dict[int(sel_family_desc)] if sel_family_desc != '-': data_filter = filter_data(data, [sel_family_desc], ['Classification'], [None]) if data_filter.shape[0]: st.write('Classificações ({} referências):'.format(data_filter.shape[0]), data_filter[['Part_Ref', 'Part_Description', 'Part_Cost', 'Part_PVP', 'Product_Group_DW', 'Classification', 'Classification_Prob']].rename(columns=options_file.column_translate_dict).head(50)) file_export(data_filter[['Part_Ref', 'Part_Description', 'Part_Cost', 'Part_PVP', 'Product_Group_DW', 'Classification', 'Classification_Prob']].rename(columns=options_file.column_translate_dict), 'Classificações_família_{}_{}'.format(sel_family_desc, current_date)) else: st.error('Não existem atualmente peças classificadas para a família escolhida.')