Skip to content

Commit

Permalink
use decorator function to show code
Browse files Browse the repository at this point in the history
  • Loading branch information
anish-work committed Jul 4, 2024
1 parent fdff6ab commit 24eb9ee
Showing 1 changed file with 28 additions and 17 deletions.
45 changes: 28 additions & 17 deletions components_doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ def render():
render_components()


def show_source_code(fn):
def wrapper(*args, **kwargs):
out = fn(*args, **kwargs)
code_block(inspect.getsource(fn))
return out

return wrapper


def get_source_code(fn: callable):
source_code = inspect.getsource(fn)
return source_code
Expand All @@ -32,18 +41,19 @@ def render_layouts():
# Full Width Layout
sub_section_title("Full Width Layout")

@show_source_code
def full_width_layout():
with gui.tag("div", className="container-fluid bg-light p-4"):
with gui.tag("div", className="row"):
with gui.tag("div", className="col-12"):
gui.html("This is a full width layout")

full_width_layout()
code_block(get_source_code(full_width_layout))

# 1/2 Layout
sub_section_title("Full Width 1/2 Layout")

@show_source_code
def half_width_layout():
with gui.tag("div", className="container-fluid p-2"):
with gui.tag("div", className="row"):
Expand All @@ -53,11 +63,11 @@ def half_width_layout():
gui.html("This is a 1/2 width layout")

half_width_layout()
code_block(get_source_code(half_width_layout))

# 1/3 Layout
sub_section_title("Full Width 1/3 Layout")

@show_source_code
def third_width_layout():
with gui.tag("div", className="container-fluid p-2"):
with gui.tag("div", className="row"):
Expand All @@ -69,12 +79,12 @@ def third_width_layout():
gui.html("This is a 1/3 width layout")

third_width_layout()
code_block(get_source_code(third_width_layout))

# Responsive 1/3 Layout
sub_section_title("Responsive 1/3 Layout")
gui.write("These columns will go full width on small devices")

@show_source_code
def responsive_third_width_layout():
with gui.tag("div", className="container-fluid p-2"):
with gui.tag("div", className="row"):
Expand All @@ -86,7 +96,6 @@ def responsive_third_width_layout():
gui.html("This is a responsive 1/3 width layout")

responsive_third_width_layout()
code_block(get_source_code(responsive_third_width_layout))


def render_content():
Expand All @@ -99,6 +108,7 @@ def render_content():
def render_headings():
sub_section_title("Headings")

@show_source_code
def render_h1_to_h6_headings():
with gui.tag("h1"):
gui.html("This is a h1 heading")
Expand All @@ -114,31 +124,31 @@ def render_h1_to_h6_headings():
gui.html("This is a h6 heading")

render_h1_to_h6_headings()
code_block(get_source_code(render_h1_to_h6_headings))

render_headings()

# RIGHT SIDE
with gui.tag("div", className="col-12 col-md-6"):
sub_section_title("Normal Text")

@show_source_code
def normal_text():
gui.write("This is a normal text")

normal_text()
code_block(get_source_code(normal_text))

sub_section_title("Link")

@show_source_code
def link():
with gui.tag("a", href="https://www.gooey.ai"):
gui.html("This is a link")

link()
code_block(get_source_code(link))

sub_section_title("Colored Text")

@show_source_code
def colored_text():
with gui.tag("p", className="text-primary"):
gui.html("This is a primary text")
Expand All @@ -156,7 +166,6 @@ def colored_text():
gui.html("This is a light text")

colored_text()
code_block(get_source_code(colored_text))


def render_components():
Expand All @@ -180,6 +189,7 @@ def render_components():
render_inputs()
section_title("File Upload Button")

@show_source_code
def file_upload():
file = gui.file_uploader(
"**Upload Any File**",
Expand All @@ -190,13 +200,13 @@ def file_upload():
)

file_upload()
code_block(get_source_code(file_upload))


def render_inputs():
section_title("Inputs")
sub_section_title("Text Area")

@show_source_code
def text_area():
gui.text_area(
"Label: Take some input from user",
Expand All @@ -208,41 +218,41 @@ def text_area():
)

text_area()
code_block(get_source_code(text_area))

sub_section_title("Multi Select")

@show_source_code
def multi_select():
gui.multiselect(
"Label: Click below to select multiple options",
["Option 1", "Option 2", "Option 3", "Option 4"],
)

multi_select()
code_block(get_source_code(multi_select))


def render_alerts():
section_title("Alerts")

@show_source_code
def success_alert():
gui.success("Yay, this is done!")

success_alert()
code_block(get_source_code(success_alert))

@show_source_code
def error_alert():
gui.error("Oops, please try again!")

error_alert()
code_block(get_source_code(error_alert))


def render_tabs_example():
section_title("Tabs")
# Rounded Tabs
sub_section_title("Rounded Tabs")

@show_source_code
def rounded_tabs():
tab1, tab2, tab3 = gui.tabs(["Tab 1", "Tab 2", "Tab 3"])
with tab1:
Expand All @@ -253,12 +263,12 @@ def rounded_tabs():
gui.write("This is tab 3 content")

rounded_tabs()
code_block(get_source_code(rounded_tabs))

# Underline Tabs
selected_tab = "Tab 1"
sub_section_title("Underline Tabs")

@show_source_code
def underline_tabs():
with gui.nav_tabs():
for name in ["Tab 1", "Tab 2", "Tab 4"]:
Expand All @@ -274,12 +284,12 @@ def underline_tabs():
gui.write("This is tab 3 content")

underline_tabs()
code_block(get_source_code(underline_tabs))


def buttons_group():
sub_section_title("Buttons")

@show_source_code
def buttons():
with gui.tag("div"):
with gui.tag("div", className="d-flex justify-content-around"):
Expand All @@ -289,16 +299,17 @@ def buttons():
gui.button("Link Button", key="test3", type="link")

buttons()
code_block(get_source_code(buttons))


def code_block(content: str):
code_lines = content.split("\n")[1:]
formatted_code = "\n".join(code_lines)
with gui.tag("div", className="mt-4"):
gui.write(
rf"""
```python %s
"""
% content,
% formatted_code,
unsafe_allow_html=True,
)

Expand Down

0 comments on commit 24eb9ee

Please sign in to comment.