emvecchi commited on
Commit
25c98c2
·
verified ·
1 Parent(s): eb77586

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -24
app.py CHANGED
@@ -65,7 +65,7 @@ function_choices = ['Broadening Discussion',
65
  default_choices = function_choices
66
 
67
  fields: List[Field] = [
68
- Field(name="patient", type="input_col", title="**Patient:** "),
69
  Field(type="expander", title="**Session Transcription:** *(expand)*", children=[
70
  Field(name="dialogue_name", type="input_col", title=""),
71
  ]),
@@ -167,7 +167,7 @@ import html as py_html
167
  from streamlit.components.v1 import html as st_html
168
 
169
  def display_dialogue_with_numbers(text: str, title="Dialogue"):
170
- with st.container(f"{title} (line numbers)", expanded=True):
171
  # optional: wrap long lines instead of horizontal scrolling
172
  st.markdown(
173
  "<style>.stCode pre {white-space: pre-wrap !important; word-break: break-word !important;}</style>",
@@ -175,26 +175,29 @@ def display_dialogue_with_numbers(text: str, title="Dialogue"):
175
  )
176
  st.code(text, language="markdown", line_numbers=True)
177
 
178
- def markdown_with_line_numbers(text: str, height: int = 500):
179
- # Number *source lines* (not wrapped visual lines)
180
- escaped = py_html.escape(text)
181
- body = "".join(f"<span>{ln or '&nbsp;'}</span>" for ln in escaped.splitlines())
182
-
183
- st_html(f"""
184
- <div class="ln"><pre>{body}</pre></div>
185
- <style>
186
- .ln {{ counter-reset: line; font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
187
- font-size: 0.95rem; border: 1px solid #ddd; border-radius: .5rem;
188
- padding: .75rem; background: #fff; max-height: 100%; overflow: auto; }}
189
- .ln pre {{ white-space: pre-wrap; margin: 0; }}
190
- .ln pre span {{ display: block; }}
191
- .ln pre span::before {{
192
- counter-increment: line;
193
- content: counter(line);
194
- display: inline-block; width: 4ch; margin-right: 1ch; text-align: right; opacity: .6;
195
- }}
196
- </style>
197
- """, height=height, scrolling=True)
 
 
 
198
 
199
  def display_image(image_path):
200
  with hf_fs.open(image_path) as f:
@@ -250,8 +253,10 @@ def show_field(f: Field, index: int, data_collected):
250
  if f.name == 'image_name':
251
  display_image(os.path.join(input_repo_path, 'images', value))
252
  elif f.name == 'dialogue_name':
253
- display_dialogue(os.path.join(input_repo_path, 'dialogues', value))
254
- #display_dialogue(os.path.join(input_repo_path, value))
 
 
255
  else:
256
  st.write(value)
257
  case 'markdown':
 
65
  default_choices = function_choices
66
 
67
  fields: List[Field] = [
68
+ Field(name="patient", type="input_col", title=" "),
69
  Field(type="expander", title="**Session Transcription:** *(expand)*", children=[
70
  Field(name="dialogue_name", type="input_col", title=""),
71
  ]),
 
167
  from streamlit.components.v1 import html as st_html
168
 
169
  def display_dialogue_with_numbers(text: str, title="Dialogue"):
170
+ with st.expander(f"{title} (line numbers)", expanded=True):
171
  # optional: wrap long lines instead of horizontal scrolling
172
  st.markdown(
173
  "<style>.stCode pre {white-space: pre-wrap !important; word-break: break-word !important;}</style>",
 
175
  )
176
  st.code(text, language="markdown", line_numbers=True)
177
 
178
+ def _enable_code_wrap_once():
179
+ if not st.session_state.get("_code_wrap_css_added"):
180
+ st.markdown(
181
+ "<style>div[data-testid='stCode'] pre {white-space: pre-wrap !important; word-break: break-word !important;}</style>",
182
+ unsafe_allow_html=True,
183
+ )
184
+ st.session_state["_code_wrap_css_added"] = True
185
+
186
+ def display_dialogue_container(hf_path: str, title: str = "Dialogue", wrap: bool = True):
187
+ # read file from HF or local (uses your existing flags/objects)
188
+ if filesystem == 'hf':
189
+ with hf_fs.open(hf_path, "rb") as f:
190
+ txt = f.read().decode("utf-8")
191
+ else:
192
+ with open(hf_path, "r", encoding="utf-8") as f:
193
+ txt = f.read()
194
+
195
+ if wrap:
196
+ _enable_code_wrap_once()
197
+
198
+ with st.container(border=True):
199
+ st.markdown(f"**{title}**")
200
+ st.code(txt, language="markdown", line_numbers=True)
201
 
202
  def display_image(image_path):
203
  with hf_fs.open(image_path) as f:
 
253
  if f.name == 'image_name':
254
  display_image(os.path.join(input_repo_path, 'images', value))
255
  elif f.name == 'dialogue_name':
256
+ #display_dialogue(os.path.join(input_repo_path, 'dialogues', value))
257
+ display_dialogue_container(os.path.join(input_repo_path, 'dialogues', value), title="Dialogue Transcription"))
258
+ elif f.name == 'patient':
259
+ st.markdown(f"**Patient:**&nbsp;&nbsp;{value}")
260
  else:
261
  st.write(value)
262
  case 'markdown':