Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import json
|
| 4 |
+
import random
|
| 5 |
+
|
| 6 |
+
# Parameters for value checking
|
| 7 |
+
parameters = {
|
| 8 |
+
"speed": 50,
|
| 9 |
+
"speed_uncertainty": 4,
|
| 10 |
+
"flow": 10.4,
|
| 11 |
+
"flow_uncertainty": 0.2,
|
| 12 |
+
"env_temperature": 35,
|
| 13 |
+
"sup_temperature": 50,
|
| 14 |
+
"gap": 0.1,
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
def check_value(parameter, value, parameters):
|
| 18 |
+
if parameter == "Velocity":
|
| 19 |
+
return parameters["speed"] - parameters["speed_uncertainty"] <= value <= parameters["speed"] + parameters["speed_uncertainty"]
|
| 20 |
+
elif parameter == "Flow":
|
| 21 |
+
return parameters["flow"] - parameters["flow_uncertainty"] <= value <= parameters["flow"] + parameters["flow_uncertainty"]
|
| 22 |
+
elif parameter == "Env. Temperature":
|
| 23 |
+
return value < parameters["env_temperature"]
|
| 24 |
+
elif parameter == "Sup. Temperature":
|
| 25 |
+
return value < parameters["sup_temperature"]
|
| 26 |
+
elif parameter == "Gap":
|
| 27 |
+
return value < parameters["gap"]
|
| 28 |
+
return None
|
| 29 |
+
|
| 30 |
+
def generate_table_html(df, parameters):
|
| 31 |
+
# Build CSS and table
|
| 32 |
+
html = """
|
| 33 |
+
<style>
|
| 34 |
+
table.custom-table {
|
| 35 |
+
width: 100%;
|
| 36 |
+
border-collapse: collapse;
|
| 37 |
+
}
|
| 38 |
+
table.custom-table th, table.custom-table td {
|
| 39 |
+
border: 1px solid #ddd;
|
| 40 |
+
padding: 8px;
|
| 41 |
+
text-align: center;
|
| 42 |
+
}
|
| 43 |
+
button.action-btn {
|
| 44 |
+
padding: 5px 10px;
|
| 45 |
+
background-color: #4CAF50;
|
| 46 |
+
color: white;
|
| 47 |
+
border: none;
|
| 48 |
+
cursor: pointer;
|
| 49 |
+
}
|
| 50 |
+
</style>
|
| 51 |
+
"""
|
| 52 |
+
html += '<table class="custom-table">\n'
|
| 53 |
+
html += " <tr>\n"
|
| 54 |
+
for col in df.columns:
|
| 55 |
+
html += f" <th>{col}</th>\n"
|
| 56 |
+
html += " <th>Details</th>\n"
|
| 57 |
+
html += " </tr>\n"
|
| 58 |
+
|
| 59 |
+
for _, row in df.iterrows():
|
| 60 |
+
row_data = row.to_dict()
|
| 61 |
+
row_data_json = json.dumps(row_data)
|
| 62 |
+
html += " <tr>\n"
|
| 63 |
+
for col in df.columns:
|
| 64 |
+
value = row[col]
|
| 65 |
+
color = check_value(col, value, parameters)
|
| 66 |
+
style = f' style="color: {"#00C005" if color else "#DE0000"};"' if color is not None else ""
|
| 67 |
+
html += f' <td{style}>{value}</td>\n'
|
| 68 |
+
# When the button is clicked, call sendToPython with the row's JSON data.
|
| 69 |
+
html += f' <td><button class="action-btn" onclick=\'sendToPython(this)\' data-row=\'{row_data_json}\' >Show Info</button></td>\n'
|
| 70 |
+
html += " </tr>\n"
|
| 71 |
+
|
| 72 |
+
html += "</table>\n"
|
| 73 |
+
return html
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
script = """
|
| 77 |
+
<script>
|
| 78 |
+
function sendToPython(btn) {
|
| 79 |
+
var data = btn.getAttribute('data-row');
|
| 80 |
+
var rowData = JSON.parse(data);
|
| 81 |
+
console.log("Sending row data to Python:", rowData);
|
| 82 |
+
var inputElem = document.querySelector('#hidden_textbox textarea');
|
| 83 |
+
if (inputElem) {
|
| 84 |
+
const event = new Event('input');
|
| 85 |
+
Object.defineProperty(event, "target", {value: inputElem})
|
| 86 |
+
inputElem.value = JSON.stringify(rowData);
|
| 87 |
+
inputElem.dispatchEvent(event);
|
| 88 |
+
} else {
|
| 89 |
+
console.error("Hidden textbox not found!");
|
| 90 |
+
}
|
| 91 |
+
}
|
| 92 |
+
</script>
|
| 93 |
+
"""
|
| 94 |
+
|
| 95 |
+
def generate_filename():
|
| 96 |
+
uf = f"UF{random.randint(1, 5)}"
|
| 97 |
+
cd = "CD"
|
| 98 |
+
pr1 = f"PR{random.randint(10, 99)}"
|
| 99 |
+
pr2 = f"PR{random.randint(10, 99)}"
|
| 100 |
+
return f"{uf}_{cd}_{pr1}_{pr2}"
|
| 101 |
+
|
| 102 |
+
# Create a sample DataFrame
|
| 103 |
+
data = {
|
| 104 |
+
"File name": [generate_filename() for _ in range(10)],
|
| 105 |
+
"Status PDF": [random.choice(["Complete", "Incomplete", "Error"]) for _ in range(10)],
|
| 106 |
+
"Start-End": [f"{random.randint(0, 12)}:00 - {random.randint(13, 23)}:59" for _ in range(10)],
|
| 107 |
+
"Env. Temperature": [round(random.uniform(15.0, 30.0), 2) for _ in range(10)],
|
| 108 |
+
"Sup. Temperature": [round(random.uniform(30.0, 50.0), 2) for _ in range(10)],
|
| 109 |
+
"Flow": [round(random.uniform(5.0, 20.0), 2) for _ in range(10)],
|
| 110 |
+
"Velocity": [round(random.uniform(0.5, 5.0), 2) for _ in range(10)],
|
| 111 |
+
"Gap": [round(random.uniform(0.1, 1.0), 2) for _ in range(10)],
|
| 112 |
+
"Observations": ["" for _ in range(10)]
|
| 113 |
+
}
|
| 114 |
+
df = pd.DataFrame(data)
|
| 115 |
+
|
| 116 |
+
def handle_click(input_value):
|
| 117 |
+
print("Button clicked, row data:", input_value)
|
| 118 |
+
return input_value
|
| 119 |
+
|
| 120 |
+
with gr.Blocks(head=script) as demo:
|
| 121 |
+
gr.Markdown("### Data Table with Action Button")
|
| 122 |
+
# Use a hidden gr.Textbox (which reliably fires change events) to capture the data.
|
| 123 |
+
hidden_textbox = gr.Textbox(value="", elem_id="hidden_textbox", visible=False)
|
| 124 |
+
# Render the table as HTML.
|
| 125 |
+
html_component = gr.HTML(generate_table_html(df, parameters), container=True)
|
| 126 |
+
hidden_textbox.input(fn=handle_click, inputs=[hidden_textbox], outputs=[hidden_textbox])
|
| 127 |
+
|
| 128 |
+
|
| 129 |
+
if __name__ == "__main__":
|
| 130 |
+
demo.launch()
|