Skip to content

Commit

Permalink
feat: support docstring for widgets
Browse files Browse the repository at this point in the history
  • Loading branch information
Yazawazi committed Jul 15, 2024
1 parent 758374f commit a3f8375
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
3 changes: 3 additions & 0 deletions backend/funix/config/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ class SwitchOption:
AUTO_READ_DOCSTRING_TO_FUNCTION_DESCRIPTION: bool = True
"""Auto read docstring to function description"""

AUTO_READ_DOCSTRING_TO_PARSE: bool = True
"""Auto parse docstring to function widgets and description"""

USE_FIXED_SESSION_KEY: Union[bool, str] = False
"""Use fixed session key"""

Expand Down
37 changes: 35 additions & 2 deletions backend/funix/decorator/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from typing import Callable, Optional, Union
from uuid import uuid4

from docstring_parser import parse
from flask import Flask
from flask_sock import Sock

Expand Down Expand Up @@ -361,11 +362,25 @@ def decorator(function: Callable) -> callable:

function_title = title if title is not None else function_name_

function_docstring_parsed = None
function_docstring = getattr(function, "__doc__")
if GlobalSwitchOption.AUTO_READ_DOCSTRING_TO_PARSE:
if function_docstring:
try:
function_docstring_parsed = parse(function_docstring)
except:
# If something goes wrong, just ignore it
# Use the original docstring
pass

function_description = description
if function_description == "" or function_description is None:
if GlobalSwitchOption.AUTO_READ_DOCSTRING_TO_FUNCTION_DESCRIPTION:
function_docstring = getattr(function, "__doc__")
if function_docstring:
if function_docstring_parsed is not None:
function_description = un_indent(
function_docstring_parsed.description
)
else:
function_description = un_indent(function_docstring)

parsed_theme = get_parsed_theme_fot_funix(theme)
Expand Down Expand Up @@ -498,6 +513,24 @@ def _function_reactive_update():
whitelist,
)

if function_docstring_parsed is not None:
if function_docstring_parsed.params:
for param_ in function_docstring_parsed.params:
if param_.arg_name in function_params:
decorated_param_ = decorated_params.get(
param_.arg_name, None
)
widget_ = param_.description.split(",")[0].strip()
if decorated_param_ is None:
decorated_params[param_.arg_name] = {
"widget": widget_,
}
else:
if "widget" not in decorated_param_: # no cover
decorated_params[param_.arg_name][
"widget"
] = widget_

safe_argument_config = {} if argument_config is None else argument_config

parse_argument_config(safe_argument_config, decorated_params, function_name)
Expand Down
4 changes: 4 additions & 0 deletions backend/funix/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@ plac>=1.3.5
gitignore-parser>=0.1.9
flask-sock>=0.7.0
SQLAlchemy>=2.0.23
docstring_parser>=0.16
matplotlib>=3.4.3
pandera>=0.17.2
pandas>=2.0.3
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ dependencies = [
"mpld3>=0.5.8",
"pandera>=0.17.2",
"pandas>=2.0.3",
"docstring_parser>=0.16",
]

[project.optional-dependencies]
Expand Down

0 comments on commit a3f8375

Please sign in to comment.