Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
731 changes: 729 additions & 2 deletions README.md

Large diffs are not rendered by default.

129 changes: 129 additions & 0 deletions examples/advanced_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
"""Advanced example showing customization options in uf.

This demonstrates:
- Custom page title and CSS
- Using the UfApp class for object-oriented interface
- Functions with more complex types
- Accessing function specs programmatically

To run this example:
python examples/advanced_example.py
"""

from uf import UfApp


def calculate_bmi(weight_kg: float, height_m: float) -> dict:
"""Calculate Body Mass Index.

Args:
weight_kg: Weight in kilograms
height_m: Height in meters

Returns:
Dictionary with BMI value and category
"""
bmi = weight_kg / (height_m ** 2)

if bmi < 18.5:
category = "Underweight"
elif bmi < 25:
category = "Normal weight"
elif bmi < 30:
category = "Overweight"
else:
category = "Obese"

return {
"bmi": round(bmi, 2),
"category": category,
}


def reverse_string(text: str, uppercase: bool = False) -> str:
"""Reverse a string.

Args:
text: The string to reverse
uppercase: Whether to convert to uppercase

Returns:
The reversed string
"""
reversed_text = text[::-1]
return reversed_text.upper() if uppercase else reversed_text


def fibonacci(n: int) -> list:
"""Generate Fibonacci sequence.

Args:
n: Number of Fibonacci numbers to generate

Returns:
List of Fibonacci numbers
"""
if n <= 0:
return []
elif n == 1:
return [0]

fib = [0, 1]
for i in range(2, n):
fib.append(fib[i-1] + fib[i-2])

return fib


# Custom CSS for a nicer look
CUSTOM_CSS = """
body {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

#sidebar {
background: rgba(255, 255, 255, 0.95);
}

#header {
background: rgba(255, 255, 255, 0.95);
}

.function-item.active {
background: #667eea;
}

.function-item:hover {
border-color: #667eea;
}
"""


if __name__ == '__main__':
# Create UfApp with customization
uf_app = UfApp(
[calculate_bmi, reverse_string, fibonacci],
page_title="Advanced Function UI",
custom_css=CUSTOM_CSS,
)

# Access function specs programmatically
print("Available functions:")
for func_name in uf_app.list_functions():
spec = uf_app.get_spec(func_name)
print(f" - {func_name}: {spec['description']}")

# Test calling functions directly
print("\nDirect function calls:")
print(f" fibonacci(10) = {uf_app.call('fibonacci', n=10)}")
print(f" reverse_string('hello') = {uf_app.call('reverse_string', text='hello')}")

print("\nStarting server at http://localhost:8080")
print("Press Ctrl+C to stop")

# Run the server
try:
uf_app.run(host='localhost', port=8080, debug=True)
except NotImplementedError:
print("For FastAPI apps, run:")
print(" uvicorn examples.advanced_example:uf_app.app --reload")
83 changes: 83 additions & 0 deletions examples/basic_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
"""Basic example of using uf to create a web UI for functions.

This demonstrates the simplest possible usage - just define functions
and pass them to mk_rjsf_app.

To run this example:
python examples/basic_example.py

Then open http://localhost:8080 in your browser.
"""

from uf import mk_rjsf_app


def add(x: int, y: int) -> int:
"""Add two numbers together.

Args:
x: First number
y: Second number

Returns:
The sum of x and y
"""
return x + y


def multiply(x: float, y: float) -> float:
"""Multiply two numbers.

Args:
x: First number
y: Second number

Returns:
The product of x and y
"""
return x * y


def greet(name: str, greeting: str = "Hello") -> str:
"""Generate a greeting message.

Args:
name: Name of the person to greet
greeting: The greeting to use (default: "Hello")

Returns:
A friendly greeting message
"""
return f"{greeting}, {name}!"


def is_even(number: int) -> bool:
"""Check if a number is even.

Args:
number: The number to check

Returns:
True if the number is even, False otherwise
"""
return number % 2 == 0


if __name__ == '__main__':
# Create the web app with all our functions
app = mk_rjsf_app(
[add, multiply, greet, is_even],
page_title="Basic Math & Text Functions"
)

# Run the server
print("Starting server at http://localhost:8080")
print("Press Ctrl+C to stop")

# For bottle apps, we can call run() directly
if hasattr(app, 'run'):
app.run(host='localhost', port=8080, debug=True)
else:
# For FastAPI apps
print("For FastAPI apps, run:")
print(" uvicorn examples.basic_example:app --reload")
Loading
Loading