Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -111,27 +111,37 @@ def dataframe_info(data):
|
|
111 |
value= data[:5]
|
112 |
return str(value)
|
113 |
|
114 |
-
def extract_python_code(text):
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
|
133 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
134 |
|
|
|
|
|
|
|
135 |
|
136 |
# @st.cache_resource
|
137 |
def run_code_blocks(code_blocks,df):
|
|
|
111 |
value= data[:5]
|
112 |
return str(value)
|
113 |
|
114 |
+
# def extract_python_code(text):
|
115 |
+
# """
|
116 |
+
# Extracts code blocks from a given text.
|
117 |
+
# Supports triple-backtick blocks, indented code, and inline code.
|
118 |
+
# """
|
119 |
+
# code_snippets = []
|
120 |
|
121 |
+
# # Extract triple-backtick code blocks
|
122 |
+
# triple_backtick_blocks = re.findall(r"```(?:[\w+\-]*)\n(.*?)```", text, re.DOTALL)
|
123 |
+
# code_snippets.extend(triple_backtick_blocks)
|
124 |
|
125 |
+
# # Extract indented code blocks (4 spaces or tab)
|
126 |
+
# indented_blocks = re.findall(r"(?:^|\n)((?: |\t).+(\n(?: |\t).+)*)", text)
|
127 |
+
# code_snippets.extend([block[0] for block in indented_blocks])
|
128 |
|
129 |
+
# # Extract inline code snippets with single backticks
|
130 |
+
# inline_code = re.findall(r"`([^`\n]+)`", text)
|
131 |
+
# code_snippets.extend(inline_code)
|
132 |
|
133 |
+
# return [snippet.strip() for snippet in code_snippets if snippet.strip()]
|
134 |
+
def extract_python_code(text):
|
135 |
+
# Match triple backtick Python code blocks ```python ... ```
|
136 |
+
code_blocks = re.findall(r"```(?:python)?\n(.*?)```", text, re.DOTALL)
|
137 |
+
|
138 |
+
# If no triple-backtick code found, try to match indented blocks (e.g. 4 spaces)
|
139 |
+
if not code_blocks:
|
140 |
+
code_blocks = re.findall(r"(?:^|\n)( .+?)(?=\n\S|\Z)", text, re.DOTALL)
|
141 |
|
142 |
+
# Clean indentation if needed
|
143 |
+
code_blocks = [re.sub(r"^\s{4}", "", block, flags=re.MULTILINE) for block in code_blocks]
|
144 |
+
return code_blocks
|
145 |
|
146 |
# @st.cache_resource
|
147 |
def run_code_blocks(code_blocks,df):
|