Skip to content

Commit

Permalink
Merge pull request #3 from manatlan/futur_with_redys
Browse files Browse the repository at this point in the history
Futur with redys
  • Loading branch information
manatlan authored Oct 3, 2023
2 parents a7c9105 + 1b26254 commit bc105bb
Show file tree
Hide file tree
Showing 24 changed files with 1,021 additions and 802 deletions.
83 changes: 79 additions & 4 deletions example.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,75 @@
from htag import Tag
import json,asyncio,time,os

class App(Tag.div):
"""
Complex htag's app to test:
- a dynamic object (TagSession), which got a render method (new way)
- using tag.state (in session)
- using tag.update with a task in a loop
- can recreate itself (when init params change)
"""

class TagSession(Tag.div): #dynamic component (compliant htag >= 0.30) !!!! FIRST IN THE WORLD !!!!
def init(self):
self+= "hello world"
self["style"]="border:1px solid black"
self.otitle = Tag.h3(_style="padding:0px;margin:0px;float:right")
self.orendu = Tag.pre(_style="padding:0px;margin:0px")

# draw ui
self+=self.otitle
self+=self.orendu

def render(self):
self.otitle.set( "Live Session" )
self.orendu.set( json.dumps( dict(self.session.items()), indent=1))

class App(Tag.body):
imports=[TagSession]
statics=b"window.error=alert"
def init(self,v="0"):
self.place = Tag.div(js="console.log('I update myself')")
asyncio.ensure_future( self.loop_timer() )

def inc_test_session(o):
v=int(self.state.get("integer","0"))
v=v+1
self.state["integer"]=v
def addd(o):
if "list" in self.state:
self.state["list"].append("x") # <= this workd because tag.state.save() called in interaction (before guess rendering)
else:
self.state["list"]=[]
def clllll(o):
self.state.clear()


self <= Tag.div(f"V{v} (pid:{os.getpid()})")
self <= Tag.button("inc integer",_onclick=inc_test_session)
self <= Tag.button("add list",_onclick=addd)
self <= Tag.button("clear",_onclick=clllll)
#~ self <= Tag.button("yield",_onclick=self.yielder)
self <= TagSession()

self+=Tag.li(Tag.a("t0",_href="/"))
self+=Tag.li(Tag.a("t1",_href="/?v=1"))
self+=Tag.li(Tag.a("t2",_href="/?v=2"))
self+=self.place

#~ async def yielder(self,o):
#~ for i in "ABCDEF":
#~ await asyncio.sleep(0.3)
#~ self+=i

async def loop_timer(self):
while 1:
await asyncio.sleep(0.5)
self.place.set(time.time() )
if not await self.place.update(): # update component using current websocket
# break if can't (<- good practice to kill this asyncio/loop)
break


# With Web http runner provided by htag
#------------------------------------------------------
Expand All @@ -11,5 +78,13 @@ def init(self):

# With htagweb.WebServer runner provided by htagweb
#------------------------------------------------------
from htagweb import AppServer
AppServer( App ).run(openBrowser=True)
from htagweb import SimpleServer,AppServer
app=AppServer( App ,parano=False,httponly=False)

if __name__=="__main__":
#~ import logging
#~ logging.basicConfig(format='[%(levelname)-5s] %(name)s: %(message)s',level=logging.DEBUG)
#~ logging.getLogger("redys.servone").setLevel( logging.INFO )


app.run(openBrowser=True)
3 changes: 2 additions & 1 deletion examples/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os,sys; sys.path.insert(0,os.path.realpath(os.path.dirname(os.path.dirname(__file__))))

from htagweb import AppServer
from htagweb import SimpleServer,AppServer
from starlette.responses import HTMLResponse

import app1
Expand Down Expand Up @@ -43,6 +43,7 @@ async def handlePath(request):
return HTMLResponse("404",404)


#app=SimpleServer()
app=AppServer()
app.add_route("/{path:path}", handlePath )

Expand Down
4 changes: 2 additions & 2 deletions examples/oauth_example.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os,sys; sys.path.insert(0,os.path.realpath(os.path.dirname(os.path.dirname(__file__))))

from htag import Tag
from htagweb import AppServer
from htagweb import SimpleServer
from authlib.integrations.starlette_client import OAuth
from starlette.responses import Response,RedirectResponse
import time
Expand Down Expand Up @@ -92,7 +92,7 @@ def render(self): # dynamic rendering
#=========================================

# IT WORKS FOR THE 3 runners of htagweb ;-) (should work with old webhttp/webws runners from htag too)
app=AppServer(App)
app=SimpleServer(App)

app.add_route("/oauth_{action}", oauth_request_action )

Expand Down
9 changes: 5 additions & 4 deletions htagweb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
# https://github.com/manatlan/htagweb
# #############################################################################

from .appserver import AppServer # a completly different beast, but compatible with ^^
__version__ = "0.0.0" # auto updated

from .appserver import AppServer
from .simpleserver import SimpleServer # a completly different beast, but compatible with ^^
from .htagserver import HtagServer # a completly different beast.
from .usot import Usot

__all__= ["AppServer"]
__all__= ["AppServer","SimpleServer"]

__version__ = "0.0.0" # auto updated
4 changes: 2 additions & 2 deletions htagweb/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@

if __name__=="__main__":
if len(sys.argv)==1:
app=HtagServer(None, debug=True,ssl=False)
app=HtagServer(None, debug=True)
elif len(sys.argv)==2:
app=HtagServer(sys.argv[1], debug=True,ssl=False)
app=HtagServer(sys.argv[1], debug=True)
else:
print("bad call (only one paremeter is possible (a fqn, ex: 'main:App'))")
sys.exit(-1)
Expand Down
Loading

0 comments on commit bc105bb

Please sign in to comment.