| 114 | |
| 115 | /** Assemble source loading, analysis and bounded JSON output into one executable script. */ |
| 116 | export function executionCode(code: string, source: DataSourceConfig, limits: ExecutionOptions): string { |
| 117 | const protectedFile = |
| 118 | 'path' in source.options && |
| 119 | typeof source.options.path === 'string' && |
| 120 | 'headers' in source.options && |
| 121 | /^https?:\/\//i.test(source.options.path) && |
| 122 | Object.keys(source.options.headers ?? {}).length > 0; |
| 123 | const load = sourceCode(source, protectedFile); |
| 124 | const loadCode = protectedFile |
| 125 | ? `${protectedFileCode}try: |
| 126 | ${load.replace(/\n/g, '\n ')} |
| 127 | finally: |
| 128 | _file.close() |
| 129 | _buffer.close() |
| 130 | del _file, _buffer` |
| 131 | : load; |
| 132 | return String.raw`import io |
| 133 | import pandas as pd |
| 134 | import contextlib |
| 135 | import json |
| 136 | import sys |
| 137 | |
| 138 | request = json.loads(${JSON.stringify(JSON.stringify({ code, options: source.options, limits }))}) |
| 139 | |
| 140 | # Keep prints out of the JSON response channel. |
| 141 | with contextlib.redirect_stdout(sys.stderr): |
| 142 | options = request['options'] |
| 143 | ${loadCode} |
| 144 | |
| 145 | for frame in tables.values(): |
| 146 | frame.columns = frame.columns.map(str) |
| 147 | if not frame.columns.is_unique: |
| 148 | raise ValueError('Column names must be unique after conversion to strings') |
| 149 | |
| 150 | namespace = {'pd': pd, 'tables': tables} |
| 151 | if len(tables) == 1: |
| 152 | namespace['df'] = next(iter(tables.values())) |
| 153 | |
| 154 | exec(request['code'], namespace) |
| 155 | if 'result' not in namespace: |
| 156 | raise ValueError('Python code must assign its output to result') |
| 157 | result = namespace['result'] |
| 158 | if isinstance(result, pd.Series): |
| 159 | frame = result.to_frame(name=str(result.name) if result.name is not None else 'value') |
| 160 | elif isinstance(result, pd.DataFrame): |
| 161 | frame = result |
| 162 | elif isinstance(result, dict): |
| 163 | frame = pd.DataFrame([result]) |
| 164 | elif isinstance(result, list): |
| 165 | frame = pd.DataFrame(result) if all(isinstance(row, dict) for row in result) else pd.DataFrame({'value': result}) |
| 166 | else: |
| 167 | frame = pd.DataFrame({'value': [result]}) |
| 168 | |
| 169 | frame.columns = frame.columns.map(str) |
| 170 | if not frame.columns.is_unique: |
| 171 | raise ValueError('Result column names must be unique') |
| 172 | limits = request['limits'] |
| 173 | rows = json.loads(frame.head(limits['maxRows']).to_json( |