Skip to content

Commit

Permalink
update README with new examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Forrest Bao authored and Yazawazi committed Feb 28, 2024
1 parent d51f019 commit c65de88
Show file tree
Hide file tree
Showing 11 changed files with 131 additions and 211 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ backend/funix/build
*.db
*.db-*
logs.jsonl
*.key
215 changes: 75 additions & 140 deletions README.md

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions examples/docstring_and_print.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from funix import funix

@funix(
print_to_web=True
)
def foo() -> None:
"""
## What a great app in Funix!
Funix won't let your docstring go to waste.
"""
print("It supports **Markdown**.")
print ("And <b>HTML</b>.")
return None
File renamed without changes.
34 changes: 22 additions & 12 deletions examples/small_games/wordle.py → examples/games/wordle.py
Original file line number Diff line number Diff line change
@@ -1,43 +1,50 @@
import random
import string
from funix import funix_class, funix_method
from funix import funix, funix_class, funix_method
from funix.hint import HTML

import json


with open("./files/words.json", "r") as f:
with open("./words.json", "r") as f:
words = json.load(f)


def random_5_letter() -> str:
@funix(
title="Get hint",
description="Get 5 random letters"
)
def random_5_letters() -> str:
"""
Get random 5 letter
Get random 5 letters
"""
return "".join(random.choices(string.ascii_lowercase, k=5))


@funix_class()
class Wordle:
@funix_method(
title="Wordle Settings",
description="Settings for the Wordle",
title="Start a game",
argument_labels={
"random_word": "Random 5-letter"
}
},
print_to_web=True
)
def __init__(self, random_word: bool = False):
"""
Start a game
"""
self.random_word = random_word

self.word = random_5_letter() if random_word else random.choice(words)
self.word = random_5_letters() if random_word else random.choice(words)
self.history = []

self.mismatch = False

print(self.word)
print("Game started. Now go to Play to play. ")

def __reset(self):
self.word = random_5_letter() if self.random_word else random.choice(words)
self.word = random_5_letters() if self.random_word else random.choice(words)
self.history = []

def __push(self, word: str):
Expand Down Expand Up @@ -70,11 +77,14 @@ def __generate(self) -> HTML:
html_code += "<span>Word mismatch, please enter a 5-letter word</span>"
if len(self.history) > 0 and self.history[-1] == self.word:
self.__reset()
html_code += "<span>Game Over, you win!</span>"
html_code += "<span>Good game, you win!</span>"
elif len(self.history) == 6:
html_code += f"<span>Game Over, answer is: <strong>{self.word}</strong></span>"
return html_code

def guess(self, word: str) -> HTML:
def Play(self, word: str) -> HTML:
"""
Enter a five-letter word and click the "Run" button to see your guess result.
"""
self.__push(word)
return self.__generate()
File renamed without changes.
50 changes: 9 additions & 41 deletions examples/new_type.py
Original file line number Diff line number Diff line change
@@ -1,46 +1,14 @@
from funix import funix, new_funix_type
import ipywidgets


@new_funix_type(
widget={
"name": "password",
"config": None
}
)
# class NewPassword(ipywidgets.Password):
class NewPassword(str):
def check_safe(self) -> bool:
if (
# len(self.value) > 6
# and any(char.isdigit() for char in self.value)
# and any(char.isupper() for char in self.value)
# and any(char.islower() for char in self.value)
len(self) > 6
and any(char.isdigit() for char in self)
and any(char.isupper() for char in self)
and any(char.islower() for char in self)
):
return True
return False


@funix(
title="New data type",
description="""
# Defining your own data type and associating a widget with it
In this example, using Python's native class definition syntax, we first define a new data type `NewPassword` inherited from the Python's standard `str` type. It has a member function `check_safe` which checks if the password is strong.
Then, we use the `new_funix_type` decorator to associate the `NewPassword` type with the `password` widget. So a string of the `NewPassword` type will not be displayed as a normal string, but hidden when being entered.
widget = {"name":"password", "config":None}
)
class blackout(str):
def print(self):
return self + " is the message."

Now you can try it below.
""",
show_source=True,
)
def password_check(password: NewPassword="Freedom!1") -> str:
safe_message = "Your password is strong."
warning_message = "Your password is weak. It needs to contain at least 6 characters, including at least one digit, one upper case letter, and one lower case letter."
def hoho(x: blackout = "Funix Rocks!") -> str:
return x.print()

return safe_message if password.check_safe() else warning_message
if __name__ == "__main__":
print (hoho(blackout('Fun')))
9 changes: 0 additions & 9 deletions examples/print_to_web.py

This file was deleted.

9 changes: 4 additions & 5 deletions examples/sine.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
import numpy

@funix.funix(
autorun=True
autorun=True,
)
def sine(omega: numpy.arange(0, 1, 0.01)) -> matplotlib.figure.Figure:
def sine(omega: funix.hint.FloatSlider(0, 4, 0.1)) -> matplotlib.figure.Figure:
fig = matplotlib.pyplot.figure()
ax = fig.add_subplot(1, 1, 1)
x = numpy.linspace(0, 12, 100)
x = numpy.linspace(0, 20, 200)
y = numpy.sin(x*omega)
ax.plot(x, y)
matplotlib.pyplot.plot(x, y, linewidth=5)
return fig
8 changes: 5 additions & 3 deletions examples/stream.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import time


def stream() -> str:
"""
This function is used to test the stream feature of Funix.
## Streaming demo in Funix
To see it, simply click the "Run" button.
"""
message = "Freedom has many difficulties and democracy is not perfect, but we have never had to put a wall up to keep our people in, to prevent them from leaving us. I want to say, on behalf of my countrymen, who live many miles away on the other side of the Atlantic, who are far distant from you, that they take the greatest pride that they have been able to share with you, even from a distance, the story of the last 18 years. I know of no town, no city, that has been besieged for 18 years that still lives with the vitality and the force and the hope and the determination of the city of West Berlin. While the wall is the most obvious and vivid demonstration of the failures of the communist system, for all the world to see, we take no satisfaction in it, for it is, as your mayor has said, an offense not only against history but an offense against humanity, separating families, dividing husbands and wives and brothers and sisters, and dividing a people who wish to be joined together. -- President John F. Kennedy at the Rudolph Wilde Platz, Berlin, June 26, 1963."
message = "We the People of the United States, in Order to form a more perfect Union, establish Justice, insure domestic Tranquility, provide for the common defence, promote the general Welfare, and secure the Blessings of Liberty to ourselves and our Posterity, do ordain and establish this Constitution for the United States of America. "

for i in range(len(message)):
time.sleep(0.01)
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "funix"
version = "0.5.4"
authors = [
{name = "Textea Inc.", email = "bao@textea.co"}
{name = "Textea Inc.", email = "hello@funix.io"}
]
license = {file = "LICENSE"}
description = "Building web apps without manually creating widgets"
Expand Down

0 comments on commit c65de88

Please sign in to comment.