Skip to content

Commit

Permalink
feat(decorator): add is_default argument
Browse files Browse the repository at this point in the history
  • Loading branch information
Yazawazi committed Oct 29, 2023
1 parent 0c300f9 commit 82973fe
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 13 deletions.
14 changes: 13 additions & 1 deletion backend/funix/decorator/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,11 @@
A dict, key is module name, value is the number of functions in the module.
"""

default_function: str | None = None
"""
Default function id.
"""

cached_list_functions: list[dict] = []
"""
Cached list functions. For `/list` route.
Expand Down Expand Up @@ -439,7 +444,7 @@ def enable_wrapper() -> None:
"""
Enable the wrapper, this will add the list and file path to the app.
"""
global __wrapper_enabled
global __wrapper_enabled, default_function
if not __wrapper_enabled:
__wrapper_enabled = True

Expand All @@ -457,6 +462,7 @@ def __funix_export_func_list() -> dict:
"""
return {
"list": make_decorated_functions_happy(),
"default_function": default_function,
}

enable_file_service()
Expand Down Expand Up @@ -571,6 +577,7 @@ def funix(
argument_config: ArgumentConfigType = None,
pre_fill: PreFillType = None,
menu: Optional[str] = None,
is_default: bool = False,
rate_limit: Limiter | list | dict = list(),
):
"""
Expand Down Expand Up @@ -605,6 +612,7 @@ def funix(
menu(str):
full module path of the function, for `path` only.
You don't need to set it unless you are funixing a directory and package.
is_default(bool): whether this function is the default function
rate_limit(Limiter | list[Limiter]): rate limiters, an object or a list
Returns:
Expand All @@ -628,6 +636,7 @@ def decorator(function: callable) -> callable:
Raises:
Check code for details
"""
global default_function
if __wrapper_enabled:
if menu:
module_functions_counter[menu] = (
Expand All @@ -644,6 +653,9 @@ def decorator(function: callable) -> callable:

function_id = str(uuid4())

if is_default:
default_function = function_id

safe_module_now = now_module

if safe_module_now:
Expand Down
24 changes: 14 additions & 10 deletions examples/themes/theme_showroom.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,36 @@
This a markdown playground in the {theme} mode theme. You can write markdown here and see the result on the right.
"""


@funix(
title="Dark",
description=description.format(theme="dark"),
theme="./dark_test.json",
show_source=True
show_source=True,
is_default=True,
)
def theme_test_markdown_playground(markdown: StrCode("markdown")) -> IPython.display.Markdown:
def theme_test_markdown_playground(
markdown: StrCode("markdown"),
) -> IPython.display.Markdown:
return markdown


@funix(
title="Sunset",
description=description.format(theme="sunset"),
theme="./sunset.json",
show_source=True
show_source=True,
)
def theme_sunset(markdown: StrCode("markdown")) -> IPython.display.Markdown:
return markdown


@funix(
title="Pink",
description=description.format(theme="pink"),
title="Pink",
description=description.format(theme="pink"),
theme="./pink.json",
show_source=True
)
show_source=True,
)
def theme_test_pink(markdown: StrCode("markdown")) -> IPython.display.Markdown:
return markdown

Expand All @@ -41,7 +45,7 @@ def theme_test_pink(markdown: StrCode("markdown")) -> IPython.display.Markdown:
title="Comic Sans",
description=description.format(theme="Comic Sans"),
theme="./comic_sans.json",
show_source=True
show_source=True,
)
def theme_comic_sans(markdown: StrCode("markdown")) -> IPython.display.Markdown:
return markdown
Expand All @@ -51,7 +55,7 @@ def theme_comic_sans(markdown: StrCode("markdown")) -> IPython.display.Markdown:
title="Kanagawa",
description=description.format(theme="Kanagawa"),
theme="./kanagawa.json",
show_source=True
show_source=True,
)
def theme_kanagawa(markdown: StrCode("markdown")) -> IPython.display.Markdown:
return markdown
return markdown
14 changes: 13 additions & 1 deletion frontend/src/components/FunixFunctionList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ const FunixFunctionList: React.FC<FunctionListProps> = ({ backend }) => {
selectedFunction: null,
}));
async function queryData() {
const { list } = await getList(new URL("/list", backend));
const { list, default_function } = await getList(
new URL("/list", backend)
);
setState(list);
setStore((store) => ({
...store,
Expand All @@ -113,6 +115,16 @@ const FunixFunctionList: React.FC<FunctionListProps> = ({ backend }) => {
if (list.length === 1) {
handleFetchFunctionDetail(list[0]);
setRadioGroupValue(list[0].path);
} else {
if (default_function !== null) {
const preview = list.filter(
(preview) => preview.id === default_function
);
if (preview.length === 1) {
handleFetchFunctionDetail(preview[0]);
setRadioGroupValue(preview[0].path);
}
}
}
}
queryData().then();
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/shared/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export type FunctionPreview = {
};

export type GetListResponse = {
default_function: null | string;
list: FunctionPreview[];
};

Expand Down Expand Up @@ -251,7 +252,6 @@ function recursiveSort(arr: (string | object)[]): (string | object)[] {
return keyA.localeCompare(keyB);
}
}
console.log(arr);
arr.sort((a, b) => compare(a, b));

for (const item of arr) {
Expand Down

0 comments on commit 82973fe

Please sign in to comment.