Skip to content

Commit b846ef6

Browse files
committed
modified cliensidecallback js strings to use r”””
1 parent 6ac00db commit b846ef6

1 file changed

Lines changed: 55 additions & 83 deletions

File tree

experiment-7/app.py

Lines changed: 55 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -573,12 +573,12 @@ def get_chat_response(prompt: str, structured_response: bool = False):
573573
)
574574

575575
app.clientside_callback(
576-
"""
576+
r"""
577577
function(n_refresh, n_date, current_date) {
578578
if (!n_refresh && !n_date) return '';
579579
580580
window.iwCounter = window.iwCounter || 0;
581-
581+
582582
const msgs = document.querySelectorAll('.bot-message:not([data-processed="true"])');
583583
584584
msgs.forEach(msg => {
@@ -593,13 +593,13 @@ def get_chat_response(prompt: str, structured_response: bool = False):
593593
594594
// Determine if this is in the community tab
595595
const isCom = !!msg.closest('#community-chat-container');
596-
596+
597597
// Get appropriate label based on tab
598598
let label;
599599
if (isCom) {
600600
// Check if this is a response to a user question or auto-generated
601601
const responseType = msg.getAttribute('data-response-type');
602-
602+
603603
if (responseType === 'user-question') {
604604
// This is a response to a user question - use the question as header
605605
const userQuestion = msg.getAttribute('data-user-question');
@@ -640,7 +640,7 @@ def get_chat_response(prompt: str, structured_response: bool = False):
640640
const content = document.createElement('div');
641641
content.className = 'collapsible-content';
642642
content.innerHTML = originalHTML;
643-
643+
644644
// Make sure content is fully visible
645645
content.style.display = 'block';
646646
content.style.overflow = 'visible';
@@ -656,13 +656,13 @@ def get_chat_response(prompt: str, structured_response: bool = False):
656656
/Streets:[\s\S]*?((?=Living Conditions:|Trash:|Parking:|$))/g,
657657
/Parking:[\s\S]*?((?=Living Conditions:|Trash:|Streets:|$))/g
658658
];
659-
659+
660660
let contentHTML = content.innerHTML;
661661
// Apply each pattern
662662
categoryPatterns.forEach(pattern => {
663663
contentHTML = contentHTML.replace(pattern, '');
664664
});
665-
665+
666666
content.innerHTML = contentHTML;
667667
} catch (e) {
668668
console.error("Error cleaning categories:", e);
@@ -902,12 +902,7 @@ def handle_chat_response(stored_input, slider_value, current_messages, selected_
902902
event_ids = ",".join(selected_hexbins_data["selected_ids"])
903903
event_id_data = get_select_311_data(event_ids=event_ids)
904904
event_date_data = get_select_311_data(event_date=selected_date)
905-
prompt += (
906-
f"\n\nYour neighbor has specifically selected an area within Dorchester to examine. "
907-
f"The overall neighborhood 311 data on {selected_date} are: {event_date_data}. "
908-
f"The specific area 311 data are: {event_id_data}. Compare the local area data, the neighborhood-wide data, "
909-
f"and the overall trends in the original 311 data."
910-
)
905+
prompt += f"\n\nYour neighbor has specifically selected an area within Dorchester to examine. " f"The overall neighborhood 311 data on {selected_date} are: {event_date_data}. " f"The specific area 311 data are: {event_id_data}. Compare the local area data, the neighborhood-wide data, " f"and the overall trends in the original 311 data."
911906
# Requesting response for 311 By the Numbers
912907
reply = get_chat_response(prompt=prompt, structured_response=True)
913908
bot_response = html.Div([dcc.Markdown(reply, dangerously_allow_html=True)], className="bot-message")
@@ -937,9 +932,9 @@ def handle_chat_input_right(n_clicks, n_submit, input_value, msgs):
937932
ctx = dash.callback_context
938933
if not ctx.triggered or not input_value or not input_value.strip():
939934
raise PreventUpdate
940-
935+
941936
msgs = msgs or []
942-
937+
943938
return msgs, {"display": "block"}, input_value.strip()
944939

945940

@@ -951,6 +946,7 @@ def handle_chat_input_right(n_clicks, n_submit, input_value, msgs):
951946
def show_right_spinner_on_slider_change(slider_value):
952947
return {"display": "block"}
953948

949+
954950
@callback(
955951
[
956952
Output("chat-messages-right", "children", allow_duplicate=True),
@@ -971,21 +967,21 @@ def show_right_spinner_on_slider_change(slider_value):
971967
def handle_chat_response_right(stored_input, slider_value, msgs, selected, refresh_clicks):
972968
msgs = msgs or []
973969
ctx = dash.callback_context
974-
trigger_id = ctx.triggered[0]['prop_id'] if ctx.triggered else None
975-
970+
trigger_id = ctx.triggered[0]["prop_id"] if ctx.triggered else None
971+
976972
year, month = date_string_to_year_month(slider_value)
977973
selected_date = f"{year}-{month:02d}"
978-
974+
979975
# Determine if this was triggered by a user question or slider change
980-
is_user_question = stored_input and trigger_id == 'user-message-store-right.data'
981-
976+
is_user_question = stored_input and trigger_id == "user-message-store-right.data"
977+
982978
# Build prompt based on trigger source
983979
if is_user_question:
984980
prompt = f"response-type = community. Your neighbor wants to hear community voices for {selected_date}. Based on the available data, provide insight in a direct and to-the-point manner to your neighbor's question: {stored_input}"
985981
else:
986982
prompt = f"response-type = community. Share community voices and concerns from {selected_date}. Write from neighbors' perspectives using first-person quotes. Avoid using category headers like 'Living Conditions:', 'Trash:', etc. Focus on personal testimonials and community sentiment. Provide detailed multi-paragraph responses with several specific examples from community members."
987983

988-
# Get area context if available
984+
# Get area context if available
989985
area_context = ""
990986
ids = selected.get("selected_ids", [])
991987
LIMIT = 150
@@ -995,11 +991,8 @@ def handle_chat_response_right(stored_input, slider_value, msgs, selected, refre
995991
evt_csv = get_select_311_data(event_ids=",".join(limited_ids))
996992
date_csv = get_select_311_data(event_date=selected_date)
997993

998-
area_context = (
999-
f"\n\nNeighbors in a specific area reported these issues: {evt_csv}\n\n"
1000-
f"Across the neighborhood on {selected_date}, reports show: {date_csv}"
1001-
)
1002-
994+
area_context = f"\n\nNeighbors in a specific area reported these issues: {evt_csv}\n\n" f"Across the neighborhood on {selected_date}, reports show: {date_csv}"
995+
1003996
if len(ids) > LIMIT:
1004997
area_context += f"\n\nNote: This area had {len(ids)} reported concerns, but only {LIMIT} are being considered due to system limits."
1005998

@@ -1009,27 +1002,19 @@ def handle_chat_response_right(stored_input, slider_value, msgs, selected, refre
10091002
reply = get_chat_response(prompt=prompt, structured_response=False)
10101003

10111004
# Add data attributes to the bot message
1012-
bot_msg_attrs = {
1013-
"data-response-type": "user-question" if is_user_question else "auto-generated",
1014-
"data-date": selected_date
1015-
}
1016-
1005+
bot_msg_attrs = {"data-response-type": "user-question" if is_user_question else "auto-generated", "data-date": selected_date}
1006+
10171007
# Only add the user question attribute if this is a response to a user question
10181008
if is_user_question:
10191009
bot_msg_attrs["data-user-question"] = stored_input
1020-
1021-
msgs.append(html.Div(
1022-
dcc.Markdown(reply, dangerously_allow_html=True),
1023-
className="bot-message",
1024-
**bot_msg_attrs
1025-
))
1010+
1011+
msgs.append(html.Div(dcc.Markdown(reply, dangerously_allow_html=True), className="bot-message", **bot_msg_attrs))
10261012

10271013
refresh_clicks = 0 if refresh_clicks is None else refresh_clicks + 1
10281014

10291015
return msgs, {"display": "none"}, refresh_clicks
10301016

10311017

1032-
10331018
@callback(
10341019
[
10351020
Output("overlay", "style", allow_duplicate=True),
@@ -1160,35 +1145,13 @@ def handle_initial_prompts(n_clicks, selected, slider_value, refresh_clicks):
11601145
if len(ids) > LIMIT:
11611146
area_context += f"\n\nNote: This area had {len(ids)} events, but only {LIMIT} are analyzed due to system limits."
11621147

1163-
stats_prompt = (
1164-
f"response-type = analytic. A by-the-numbers overview for Dorchester on {selected_date}:{area_context} "
1165-
"Your neighbor has selected this specific area to focus on. You don't have to compare the statistics but just analyze the data and give the statistics along with insights. Focus on counts of 311, shots fired, etc."
1166-
)
1148+
stats_prompt = f"response-type = analytic. A by-the-numbers overview for Dorchester on {selected_date}:{area_context} " "Your neighbor has selected this specific area to focus on. You don't have to compare the statistics but just analyze the data and give the statistics along with insights. Focus on counts of 311, shots fired, etc."
11671149
stats_reply = get_chat_response(prompt=stats_prompt, structured_response=True)
1168-
stats_message = html.Div(
1169-
[html.Strong("A by-the-numbers overview of your neighborhood:"), dcc.Markdown(stats_reply, dangerously_allow_html=True)],
1170-
className="bot-message",
1171-
**{
1172-
"data-response-type": "auto-generated",
1173-
"data-date": selected_date
1174-
}
1175-
)
1150+
stats_message = html.Div([html.Strong("A by-the-numbers overview of your neighborhood:"), dcc.Markdown(stats_reply, dangerously_allow_html=True)], className="bot-message", **{"data-response-type": "auto-generated", "data-date": selected_date})
11761151

1177-
community_prompt = (
1178-
f"response-type = community. Share voices from {selected_date} community meetings:{area_context} "
1179-
"Write from the perspective of residents using first-person quotes. Avoid using category "
1180-
"headers like 'Living Conditions:', 'Trash:', etc. Focus on personal testimonials and community sentiment. "
1181-
"Provide detailed multi-paragraph responses with several specific examples from community members."
1182-
)
1152+
community_prompt = f"response-type = community. Share voices from {selected_date} community meetings:{area_context} " "Write from the perspective of residents using first-person quotes. Avoid using category " "headers like 'Living Conditions:', 'Trash:', etc. Focus on personal testimonials and community sentiment. " "Provide detailed multi-paragraph responses with several specific examples from community members."
11831153
community_reply = get_chat_response(prompt=community_prompt, structured_response=False)
1184-
community_message = html.Div(
1185-
[html.Strong("From recent community meetings:"), dcc.Markdown(community_reply, dangerously_allow_html=True)],
1186-
className="bot-message",
1187-
**{
1188-
"data-response-type": "auto-generated",
1189-
"data-date": selected_date
1190-
}
1191-
)
1154+
community_message = html.Div([html.Strong("From recent community meetings:"), dcc.Markdown(community_reply, dangerously_allow_html=True)], className="bot-message", **{"data-response-type": "auto-generated", "data-date": selected_date})
11921155
refresh_clicks = 0 if refresh_clicks is None else refresh_clicks + 1
11931156

11941157
return [stats_message], [community_message], refresh_clicks
@@ -1311,6 +1274,7 @@ def handle_combined_chat_input(n_clicks, n_submit, input_value, active_tab):
13111274

13121275
return "", right_input, right_submit, right_clicks, left_loading, right_loading
13131276

1277+
13141278
@callback(
13151279
[
13161280
Output("chat-input-combined", "value", allow_duplicate=True),
@@ -1325,6 +1289,7 @@ def update_chat_input_from_trigger(trigger_value):
13251289

13261290
return dash.no_update, dash.no_update
13271291

1292+
13281293
@callback(
13291294
Output("area-category-counts-store", "data"),
13301295
[
@@ -1426,48 +1391,54 @@ def render_category_pie(counts):
14261391
def render_counts(shots_count, homicides_count):
14271392
# Create a consistent style for both icons
14281393
bullet_style = {
1429-
"fontSize": "34px",
1394+
"fontSize": "34px",
14301395
"marginRight": "4px",
14311396
"display": "inline-block",
14321397
"verticalAlign": "middle",
14331398
"lineHeight": "1",
14341399
}
1435-
1400+
14361401
# Create consistent text style
14371402
text_style = {
1438-
"display": "inline-block",
1403+
"display": "inline-block",
14391404
"verticalAlign": "middle",
14401405
}
1441-
1406+
14421407
# Create consistent item container style
14431408
item_style = {
14441409
"display": "flex",
1445-
"alignItems": "center",
1410+
"alignItems": "center",
14461411
"justifyContent": "center",
14471412
"margin": "0 1rem",
14481413
}
14491414

14501415
# Use proper singular/plural forms for shots fired
14511416
shots_text = "Shot Fired" if shots_count == 1 else "Shots Fired"
1452-
1417+
14531418
# Shots fired with consistent styling
1454-
shots_span = html.Div([
1455-
html.Span("• ", style={**bullet_style, "color": "#701238"}),
1456-
html.Span(f"{shots_count} {shots_text}", style=text_style),
1457-
], style=item_style)
1458-
1419+
shots_span = html.Div(
1420+
[
1421+
html.Span("• ", style={**bullet_style, "color": "#701238"}),
1422+
html.Span(f"{shots_count} {shots_text}", style=text_style),
1423+
],
1424+
style=item_style,
1425+
)
1426+
14591427
# Initialize items list with shots span
14601428
items = [shots_span]
1461-
1429+
14621430
# Only add homicide count if it's greater than 0
14631431
if homicides_count and homicides_count > 0:
14641432
# Use proper singular/plural forms for homicides
14651433
homicide_text = "Homicide" if homicides_count == 1 else "Homicides"
1466-
1467-
hom_span = html.Div([
1468-
html.Span("• ", style={**bullet_style, "color": "#000000"}),
1469-
html.Span(f"{homicides_count} {homicide_text}", style=text_style),
1470-
], style=item_style)
1434+
1435+
hom_span = html.Div(
1436+
[
1437+
html.Span("• ", style={**bullet_style, "color": "#000000"}),
1438+
html.Span(f"{homicides_count} {homicide_text}", style=text_style),
1439+
],
1440+
style=item_style,
1441+
)
14711442
items.append(hom_span)
14721443

14731444
return html.Div(
@@ -1481,17 +1452,18 @@ def render_counts(shots_count, homicides_count):
14811452
"alignItems": "center",
14821453
"justifyContent": "center",
14831454
"boxShadow": "0 1px 3px rgba(0,0,0,0.1)",
1484-
}
1455+
},
14851456
),
14861457
style={
14871458
"display": "flex",
14881459
"justifyContent": "center",
14891460
"alignItems": "center",
14901461
"marginTop": "0.5rem",
14911462
"fontSize": "16px",
1492-
}
1463+
},
14931464
)
14941465

1466+
14951467
server = app.server
14961468

14971469
# Run the app

0 commit comments

Comments
 (0)