API Reference
func_to_web
cleanup_temp_file(file_id)
Remove temp file and its registry entry.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_id | str | Unique identifier for the file. | required |
Source code in func_to_web\__init__.py
create_response_with_files(processed)
Create JSON response with file downloads.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
processed | dict[str, Any] | Processed result from process_result(). | required |
Returns:
| Type | Description |
|---|---|
dict[str, Any] | Response dictionary with file IDs and metadata. |
Source code in func_to_web\__init__.py
get_temp_file(file_id)
Get temp file info from registry.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_id | str | Unique identifier for the file. | required |
Returns:
| Type | Description |
|---|---|
dict[str, str] | None | Dictionary with 'path' and 'filename' keys, or None if not found. |
Source code in func_to_web\__init__.py
handle_form_submission(request, func, params) async
Handle form submission for any function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request | Request | FastAPI request object. | required |
func | Callable | Function to call with validated parameters. | required |
params | dict[str, ParamInfo] | Parameter metadata from analyze(). | required |
Returns:
| Type | Description |
|---|---|
JSONResponse | JSON response with result or error. |
Source code in func_to_web\__init__.py
register_temp_file(file_id, path, filename)
Register a temp file for download.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_id | str | Unique identifier for the file. | required |
path | str | File system path to the temporary file. | required |
filename | str | Original filename for download. | required |
Source code in func_to_web\__init__.py
run(func_or_list, host='0.0.0.0', port=8000, template_dir=None)
Generate and run a web UI for one or more Python functions.
Single function mode: Creates a form at root (/) for the function. Multiple functions mode: Creates an index page with links to individual function forms.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func_or_list | Callable[..., Any] | list[Callable[..., Any]] | A single function or list of functions to wrap. | required |
host | str | Server host address (default: "0.0.0.0"). | '0.0.0.0' |
port | int | Server port (default: 8000). | 8000 |
template_dir | str | Path | None | Optional custom template directory. | None |
Raises:
| Type | Description |
|---|---|
FileNotFoundError | If template directory doesn't exist. |
TypeError | If function parameters use unsupported types. |
Source code in func_to_web\__init__.py
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 | |
save_uploaded_file(uploaded_file, suffix) async
Save an uploaded file to a temporary location.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
uploaded_file | Any | The uploaded file object from FastAPI. | required |
suffix | str | File extension to use for the temp file. | required |
Returns:
| Type | Description |
|---|---|
str | Path to the saved temporary file. |
Source code in func_to_web\__init__.py
func_to_web.types
func_to_web.analyze_function
ParamInfo dataclass
Metadata about a function parameter extracted by analyze().
This dataclass stores all the information needed to generate form fields, validate input, and call the function with the correct parameters.
Attributes:
| Name | Type | Description |
|---|---|---|
type | type | The base Python type of the parameter. Must be one of: int, float, str, bool, date, or time. Example: int, str, date |
default | Any | The default value specified in the parameter. - None if the parameter has no default - The actual default value if specified (e.g., 42, "hello", True) - Independent of is_optional (a parameter can be optional with or without a default) Example: For |
field_info | Any | Additional metadata from Pydantic Field or Literal. - For Annotated types: Contains the Field object with constraints (e.g., Field(ge=0, le=100) for numeric bounds, Field(min_length=3) for strings) - For Literal types: Contains the Literal type with valid options - None for basic types without constraints Example: Field(ge=18, le=100) for age constraints Example: Literal['light', 'dark'] for dropdown options |
dynamic_func | Any | Function for dynamic Literal options. - Only set for Literal[callable] type hints - Called at runtime to generate dropdown options dynamically - Returns a list, tuple, or single value - None for static Literals or non-Literal types Example: A function that returns database options |
is_optional | bool | Whether the parameter type includes None. - True for Type | None or Union[Type, None] syntax - False for regular required parameters (even if they have a default) - Affects UI: optional fields get a toggle switch to enable/disable - Default: False Example: |
optional_enabled | bool | Initial state of optional toggle. - Only relevant when is_optional=True - True: toggle starts enabled (field active) - False: toggle starts disabled (field inactive, sends None) - Determined by: explicit marker > default value > False - Default: False Example: |
is_list | bool | Whether the parameter is a list type. - True for list[Type] syntax - False for regular parameters - When True, 'type' contains the item type, not list - Default: False |
list_field_info | Any | Metadata for the list itself (min_items, max_items). - Only relevant when is_list=True - Contains Field constraints for the list container - None if no list-level constraints Example: Field(min_items=2, max_items=5) |
Source code in func_to_web\analyze_function.py
analyze(func)
Analyze a function's signature and extract parameter metadata.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func | Callable[..., Any] | The function to analyze. | required |
Returns:
| Type | Description |
|---|---|
dict[str, ParamInfo] | Mapping of parameter names to ParamInfo objects. |
Raises:
| Type | Description |
|---|---|
TypeError | If parameter type is not supported. |
TypeError | If list has no type parameter. |
TypeError | If list item type is not supported. |
TypeError | If list of Literal is used (conceptually confusing). |
TypeError | If list default is not a list. |
TypeError | If list default items have wrong type. |
ValueError | If default value doesn't match Literal options. |
ValueError | If Literal options are invalid. |
ValueError | If Union has multiple non-None types. |
ValueError | If default value type doesn't match parameter type. |
Source code in func_to_web\analyze_function.py
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 | |
func_to_web.build_form_fields
build_form_fields(params_info)
Build form field specifications from parameter metadata.
Re-executes dynamic functions to get fresh options.
This function takes the analyzed parameter information from analyze() and converts it into a list of field specifications that can be used by the template engine to generate HTML form inputs.
Process
- Iterate through each parameter's ParamInfo
- Determine the appropriate HTML input type (text, number, select, etc.)
- Extract constraints and convert them to HTML attributes
- Handle special cases (optional fields, dynamic literals, files, etc.)
- Serialize defaults to JSON-safe format
- Return list of field dictionaries ready for template rendering
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params_info | dict | Mapping of parameter names to ParamInfo objects. Keys are parameter names (str), values are ParamInfo objects with type, default, field_info, etc. | required |
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]] | List of field dictionaries for template rendering. Each dictionary contains: |
list[dict[str, Any]] |
|
list[dict[str, Any]] |
|
list[dict[str, Any]] |
|
list[dict[str, Any]] |
|
list[dict[str, Any]] |
|
list[dict[str, Any]] |
|
list[dict[str, Any]] |
|
list[dict[str, Any]] |
|
list[dict[str, Any]] |
|
list[dict[str, Any]] |
|
list[dict[str, Any]] |
|
list[dict[str, Any]] |
|
list[dict[str, Any]] |
|
list[dict[str, Any]] |
|
list[dict[str, Any]] |
|
Field Type Detection
- Literal types → 'select' (dropdown)
- bool → 'checkbox'
- date → 'date' (date picker)
- time → 'time' (time picker)
- int/float → 'number' (with constraints)
- str with file pattern → 'file' (file upload)
- str with color pattern → 'color' (color picker)
- str with email pattern → 'email' (email input)
- str (default) → 'text' (text input)
Source code in func_to_web\build_form_fields.py
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 | |
serialize_for_json(value)
Serialize a value to be JSON-safe for template rendering.
Converts date/time objects to ISO format strings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value | Any | The value to serialize (can be any type). | required |
Returns:
| Type | Description |
|---|---|
Any | JSON-safe serialized value (str for dates/times, or original type). |
Source code in func_to_web\build_form_fields.py
func_to_web.process_result
process_result(result)
Convert function result to appropriate display format.
Handles images (PIL, Matplotlib), single/multiple files, and text. Returns a dictionary with 'type' and relevant data. Files are saved to temporary files and paths are returned.
Source code in func_to_web\process_result.py
func_to_web.validate_params
validate_list_param(value, info, param_name)
Validate and convert a JSON string to a typed list.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value | str | None | JSON string like "[1, 2, 3]" or "[]". | required |
info | ParamInfo | ParamInfo with type and constraints for list items. | required |
param_name | str | Name of the parameter (for error messages). | required |
Returns:
| Type | Description |
|---|---|
list | Validated list with proper types. |
Raises:
| Type | Description |
|---|---|
TypeError | If value is not a valid list. |
ValueError | If items don't pass validation or list size constraints are violated. |
JSONDecodeError | If JSON is invalid. |
Source code in func_to_web\validate_params.py
validate_params(form_data, params_info)
Validate and convert form data to function parameters.
This function takes raw form data (where everything is a string) and converts it to the proper Python types based on the parameter metadata from analyze(). It handles type conversion, optional field toggles, and validates against constraints defined in Pydantic Field or Literal types.
Process
- Check if optional fields are enabled via toggle
- Convert strings to proper types (int, float, date, time, bool)
- For lists: parse JSON and validate each item
- Validate Literal values against allowed options
- Validate against Pydantic Field constraints (ge, le, min_length, etc.)
- Handle special cases (hex color expansion, empty values)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
form_data | dict | Raw form data from HTTP request. - Keys are parameter names (str) - Values are form values (str, or None for checkboxes) - For lists: JSON string like "[1, 2, 3]" - Optional toggles have keys like "{param}_optional_toggle" | required |
params_info | dict[str, ParamInfo] | Parameter metadata from analyze(). - Keys are parameter names (str) - Values are ParamInfo objects with type and validation info | required |
Returns:
| Type | Description |
|---|---|
dict | Validated parameters ready for function call. |
dict | Keys are parameter names (str), values are properly typed Python objects. |
Raises:
| Type | Description |
|---|---|
ValueError | If a value doesn't match Literal options or Field constraints. |
TypeError | If type conversion fails. |
JSONDecodeError | If list JSON is invalid. |
Source code in func_to_web\validate_params.py
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 | |
validate_single_item(item, info)
Validate a single list item.
Reuses the same validation logic as non-list parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item | Any | The item value from the JSON array. | required |
info | ParamInfo | ParamInfo with type and constraints. | required |
Returns:
| Type | Description |
|---|---|
Any | Validated and converted item. |