Skip to content

Commit

Permalink
[+]Litchi 0.2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
streetartist committed Jul 10, 2021
1 parent 2be2968 commit 8333cf2
Show file tree
Hide file tree
Showing 21 changed files with 490 additions and 1 deletion.
85 changes: 85 additions & 0 deletions demo/domath.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
from sys import path

path.append(r"C:/Users/yuzhou/Desktop/Litchi-main/")

from litchi.app import App
from litchi.uix.button import Button
from litchi.lang.brython import brython, Brython
from litchi.uix.html import HTML

class IndexApp(App):
@brython
def code():
from browser import document, html

# Construction de la calculatrice
calc = html.TABLE()
calc <= html.TR(html.TH(html.DIV("0", id="result"), colspan=3) +
html.TD("C"))
lines = ["789/", "456*", "123-", "0.=+"]

calc <= (html.TR(html.TD(x) for x in line) for line in lines)

document <= calc

result = document["result"] # direct acces to an element by its id

def action(event):
"""Handles the "click" event on a button of the calculator."""
# The element the user clicked on is the attribute "target" of the
# event object
element = event.target
# The text printed on the button is the element's "text" attribute
value = element.text
if value not in "=C":
# update the result zone
if result.text in ["0", "error"]:
result.text = value
else:
result.text = result.text + value
elif value == "C":
# reset
result.text = "0"
elif value == "=":
# execute the formula in result zone
try:
result.text = eval(result.text)
except:
result.text = "error"

# Associate function action() to the event "click" on all buttons
for button in document.select("td"):
button.bind("click", action)

style='''
<style>
*{
font-family: sans-serif;
font-weight: normal;
font-size: 1.1em;
}
td{
background-color: #ccc;
padding: 10px 30px 10px 30px;
border-radius: 0.2em;
text-align: center;
cursor: default;
}
#result{
border-color: #000;
border-width: 1px;
border-style: solid;
padding: 10px 30px 10px 30px;
text-align: right;
}
</style>
'''
def press(self):
return "Oh,do not press me!!"
def build(self):
self.button = Button(text="Hello,litchi!", on_pressed=self.press, id = "Button1")
self.webcode = Brython(function=self.code)
self.html = HTML(code=self.style)
return [self.button,self.webcode,self.html]

IndexApp().run(model='server').run()
11 changes: 11 additions & 0 deletions demo/first.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from litchi.app import App
from litchi.uix.button import Button

class IndexApp(App):
def press(self):
self.button.text("Oh,do not press me!!")
def build(self):
self.button = Button(text="Hello,litchi!", on_pressed=self.press, hold="")
return [self.button,]

print(IndexApp().run(model="server"))
26 changes: 26 additions & 0 deletions demo/newyear.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from litchi.app import App
from litchi.uix.label import H1
from litchi.uix.html import HTML
from litchi.uix.button import Button
from litchi.lang.brython import brython, Brython

class IndexApp(App):
@brython
def code():
from browser import document
import random
document <= html.B("新年快乐",id="wish")
wish = [
"一岁一礼,一寸欢喜。",
"新年到,往事清零,开心延续!",
"百毒不侵年年好,牛转钱坤步步高!"
]
def action(event):
document["wish"].text = wish[random.randit(0,2)]
for button in document.select("button"):
button.bind("click", action)
def build(self):
self.webcode = Brython(function=self.code)
return [self.webcode]

IndexApp().run(model='server').run()
21 changes: 21 additions & 0 deletions demo/test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!DOCTYPE HTML>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://ajax.aspnetcdn.com/ajax/jquery/jquery-3.5.1.min.js"></script>
<link href="https://cdn.bootcdn.net/ajax/libs/pure/2.0.3/pure-min.css" rel="stylesheet">
</head>
<body>
<button class="pure-button pure-button-primary" id="Button1">Hello!</button>
<script>
$('#Button1').click(function(){
$.ajax({
url:"http://127.0.0.1:5000/ajax/Button1/click",
type:"post",
success:function(result){
$("#Button1").html(result);
}});
});
</script>
</body>
</html>
20 changes: 20 additions & 0 deletions demo/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from sys import path

path.append(r"C:/Users/yuzhou/Desktop/Litchi-main/")

from litchi.app import App
from litchi.uix.button import Button
from litchi.lang.brython import brython, Brython

class IndexApp(App):
@brython
def code():
from browser import document
document <= "Hello !"
def press(self):
return "Oh,do not press me!!"
def build(self):
self.button = Button(text="Hello,litchi!", on_pressed="self.press", id = "Button1")
self.webcode = Brython(function=self.code)

IndexApp().run(model='server')
35 changes: 35 additions & 0 deletions demo/webtest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from flask import Flask, jsonify

app = Flask(__name__)

@app.route('/ajax/<id>/<event>', methods=['POST','GET'])
def ajax(id, event):
return jsonify('opsssss')

@app.route('/')
def indexs():
return '''
<!DOCTYPE HTML>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://ajax.aspnetcdn.com/ajax/jquery/jquery-3.5.1.min.js"></script>
<link href="https://cdn.bootcdn.net/ajax/libs/pure/2.0.3/pure-min.css" rel="stylesheet">
</head>
<body>
<button class="pure-button pure-button-primary" id="Button1">Hello!</button>
<script>
$('#Button1').click(function(){
$.ajax({
url:"http://127.0.0.1:5000/ajax/Button1/click",
type:"post",
success:function(result){
$("#Button1").html(result);
}});
});
</script>
</body>
</html>
'''

app.run()
5 changes: 5 additions & 0 deletions litchi/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from litchi.app import App
from litchi.uix.button import Button
from litchi.lang.brython import brython, Brython
from litchi.uix.html import HTML
from litchi.uix.label import A, H1, P, Img
Binary file added litchi/__pycache__/app.cpython-37.pyc
Binary file not shown.
63 changes: 63 additions & 0 deletions litchi/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# App code goes there.
from flask import Flask, jsonify, session

def del_waste(self, wastes):
waste = ['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__',
'__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'build', 'run']

production = []

for i in wastes:
j = getattr(self, i)

if i not in waste and hasattr(j, "litchi") == True:
production.append(j)

return production

class App:
def run(self, model, *args):
# elements = self.build()
self.build()

elements = del_waste(self, dir(self))

print(elements)

html_begin = '''
<!DOCTYPE HTML>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script type="text/javascript"
src="https://cdn.jsdelivr.net/npm/brython@3.9.1/brython.min.js">
</script>
<script src="https://ajax.aspnetcdn.com/ajax/jquery/jquery-3.5.1.min.js"></script>
<link href="https://cdn.bootcdn.net/ajax/libs/pure/2.0.3/pure-min.css" rel="stylesheet">
</head>
<body onload="brython()">
'''
# To use Brython,use<script type="text/python">You code</script>
for element in elements:
html_begin += element.convert()
html = html_begin + '''
</body>
</html>
'''
if model== "static":
return html
else:
app = Flask(__name__)

@app.route('/',methods=['GET','POST'])
def web():
return html

@app.route('/ajax/<id>/<event>', methods=['POST','GET'])
def ajax(id, event):
for element in elements:
if element.id == id:
if event == 'click':
return jsonify(element.on_pressed())

return app.run(*args)
Binary file added litchi/lang/__pycache__/base.cpython-37.pyc
Binary file not shown.
Binary file added litchi/lang/__pycache__/brython.cpython-37.pyc
Binary file not shown.
12 changes: 12 additions & 0 deletions litchi/lang/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import inspect

def get_source(func):
code=""
first = True
for line in inspect.getsourcelines(func)[0][2:]:
if first:
code+=line[4:]
first=False
else:
code+=line
return code
17 changes: 17 additions & 0 deletions litchi/lang/brython.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from litchi.lang.base import get_source

def brython(func):
code = get_source(func)
return code

class Brython:
def __init__(self, function):
self.function = function
self.litchi = None

def convert(self):
return '''
<script type="text/python">
{code}
</script>
'''.format(code=self.function)
13 changes: 13 additions & 0 deletions litchi/lang/server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from litchi.lang.base import get_source

def server(func):
function = get_source(func)

re.search("\s\S*\s*=\s*\s\S*",function) # 将变量定义加上session

for i in result.groups():
re.search("\w*",i)
function.replace(i,"session["+i+"])

return function

Binary file added litchi/uix/__pycache__/button.cpython-37.pyc
Binary file not shown.
Binary file added litchi/uix/__pycache__/html.cpython-37.pyc
Binary file not shown.
37 changes: 37 additions & 0 deletions litchi/uix/button.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Button code goes there.
class Button:
def __init__(self, text, on_pressed, id, hold="server"):
self.text = text
self.id = id
self.on_pressed = on_pressed
self.litchi = None #! 用于确认是否是Litch对象

def convert(self):
if not callable(self.on_pressed):
return '''
<button class="pure-button pure-button-primary" id="{id}">{text}</button>
<script>
$('#{id}').click(function(){{
$("#{id}").html("{change}");
}});
</script>
'''.format(id=self.id, text=self.text, change = self.on_pressed)
else:
return '''
<button class="pure-button pure-button-primary" id="{id}">{text}</button>
<script>
$('#{id}').click(function(){{
$.ajax({{
url:"/ajax/{id}/click",
type:"post",
success:function(result){{
$("#{id}").html(result);
}}
}});
}});
</script>
'''.format(id=self.id, text=self.text)
# <button class="pure-button pure-button-primary" id="'+self.id+'">' + self.text + "</button>"+

def text(self,change):
return change
6 changes: 6 additions & 0 deletions litchi/uix/html.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class HTML:
def __init__(self, code):
self.code=code
self.litchi = None
def convert(self):
return self.code
Loading

0 comments on commit 8333cf2

Please sign in to comment.