min_indi / min_indi.py
drkvcsstvn's picture
Update min_indi.py
7d0b0dd verified
#!/usr/bin/env python
# coding: utf-8
import os
import json
import urllib.parse
from datetime import datetime
from io import BytesIO
import threading
import pandas as pd
import httpx
import requests
import panel as pn
import hvplot.pandas
import holoviews as hv
pn.extension('tabulator', inline=True, sizing_mode="stretch_both")
# -------------------------------
# SETTINGS
# -------------------------------
n_start = "2024-01-01"
n_end = "2025-11-13"
n_start_datetime = datetime.strptime(n_start, "%Y-%m-%d")
n_end_datetime = datetime.strptime(n_end, "%Y-%m-%d")
parquet_path = f"{n_start}_{n_end}.parquet"
# -------------------------------
# PANEL FUNCTIONS
# -------------------------------
def tablazat(text_input, multi_select, select, table_size, orvos_input):
filtered = min_indi.groupby(multi_select).agg({
f'Esetszam {n_start[:4]}': 'sum',
f'Esetszam {n_end[:4]}': 'sum',
f'Minoseg indikator arany {n_start[:4]}(%)': 'mean',
f'Minoseg indikator arany {n_end[:4]}(%)': 'mean',
'Minoseg indikator arany kulonbseg(%)': 'mean',
'Minoseg indikator egyeb arany kulonbseg(%)': 'mean',
}).reset_index()
# Sort entire table first by selected column (descending)
sorted_df = filtered.sort_values(by=select, ascending=False).reset_index(drop=True)
# Step 3 — Determine mask based on input
if text_input:
mask = sorted_df['Bekuldo Intezet'].str.contains(text_input, case=False, na=False)
elif orvos_input:
mask = sorted_df['Bekuldo Orvos'].str.contains(orvos_input, case=False, na=False)
else:
mask = pd.Series(False, index=sorted_df.index)
# Step 4 — Get integer index positions (→ now mask becomes index list)
match_indices = sorted_df.index[mask].to_list()[:2]
# Step 5 — Collect ±3 neighbors around each match
neighbor_indices = set()
for idx in match_indices:
start = max(idx - 3, 0)
end = min(idx + 3, len(sorted_df) - 1)
neighbor_indices.update(range(start, end + 1))
# Step 6 — Build neighbor-filtered dataframe
if not neighbor_indices:
neighbor_filtered = sorted_df.head(table_size)
else:
neighbor_filtered = sorted_df.loc[sorted(neighbor_indices)]
return neighbor_filtered
# Plot
accept_color = '#5B91D1'
non_accept_color = '#CF6044'
def plot(text_input,multi_select,select,table_size,orvos_input):
filtered_min_indi= tablazat(text_input,multi_select,select,table_size,orvos_input)
# Define a function to determine the color based on the value
def get_color(value):
if value > 10:
return non_accept_color
else:
return accept_color
# Add a new column for color based on the values in column_1
filtered_min_indi['color'] = filtered_min_indi[select].apply(get_color)
bar = filtered_min_indi.hvplot.bar(
x=multi_select[0],
y=select,
color ='color',
responsive=True,
rot = 45,
ylim= [(filtered_min_indi[select].min()-1),(filtered_min_indi[select].max()+1)]
)
if filtered_min_indi[select].min() < 0:
acceptrange= hv.HSpan((filtered_min_indi[select].min()-1),10).opts(color= accept_color,alpha=0.1)
else:
acceptrange= hv.HSpan(0,10).opts(color='blue',alpha=0.2)
non_acceptrange = hv.HSpan(10,(filtered_min_indi[select].max()+1)).opts(color= non_accept_color,alpha=0.1)
return bar * acceptrange * non_acceptrange
min_indi = pd.read_parquet(parquet_path)
# -------------------------------
# WIDGETS
# -------------------------------
#esetszam_input = pn.widgets.IntInput(name='Minimum esetszám', value=5, step=1, start=0, end=500)
table_size = pn.widgets.IntSlider(name='Tábla méret', start=5, end=50, step=1, value=10)
index_options = ['Bekuldo Intezet','Bekuldo Orvos','Eloszuro','Orvos']
select = pn.widgets.Select(name='Sort by:', value = 'Minoseg indikator arany kulonbseg(%)',
options=list(min_indi.columns[4:]))
multi_select = pn.widgets.MultiSelect(name='Select Indexes', value=[index_options[2]], options=index_options, size=8)
text_input = pn.widgets.TextInput(name='Beküldő Intézet kereső', placeholder='Tartalmazza ezt...', visible=False)
orvos_input = pn.widgets.TextInput(name='Beküldő Orvos kereső', placeholder='Tartalmazza ezt...',visible=False)
def update_search_visibility(multi_select_value):
text_input.visible = index_options[0] in multi_select_value
orvos_input.visible = index_options[1] in multi_select_value
multi_select.param.watch(lambda event: update_search_visibility(event.new), 'value')
inter_ind = pn.bind(tablazat,text_input,multi_select,select,table_size,orvos_input)
inter_plot = pn.bind(plot,text_input,multi_select,select,table_size,orvos_input)
# -------------------------------
# PANEL TEMPLATE
# -------------------------------
template = pn.template.FastListTemplate(
title='Minőség Indikátorok',
sidebar=pn.Column(
text_input,
orvos_input,
multi_select,
select,
table_size,
),
main=[pn.Tabs(('DF',pn.widgets.DataFrame(inter_ind, disabled=False)),('Plot',inter_plot))],
header_background=accept_color
)
template.servable()