Skip to content

Commit

Permalink
__main__ can launch in debug mode
Browse files Browse the repository at this point in the history
  • Loading branch information
manatlan committed Feb 25, 2024
1 parent 6f51a38 commit d22aa5d
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 31 deletions.
66 changes: 39 additions & 27 deletions htag/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,9 @@
# https://github.com/manatlan/htag
# #############################################################################

import os,stat

try:
import pip

# install uvicorn if not present (for DevApp mode)
try:
import uvicorn
except ImportError as e:
pip.main(['install', 'uvicorn[standard]'])

# install starlette if not present (for DevApp mode)
try:
import starlette
except ImportError as e:
pip.main(['install', 'starlette'])

devappmode = True
except:
devappmode = False
import os,sys

devappmode=False

code = """
# -*- coding: utf-8 -*-
Expand All @@ -51,12 +33,42 @@ def init(self):
app.run()
""" % (devappmode and "from htag.runners import DevApp as Runner" or "from htag.runners import BrowserHTTP as Runner")

newfile = "main.py"
if __name__=="__main__":
if len(sys.argv)>1:
##########################################################################
## run mode
##########################################################################
htagfile=os.path.realpath(sys.argv[1])

try:
import importlib.util
module_name=os.path.basename(htagfile)[:-3]
spec = importlib.util.spec_from_file_location(module_name, htagfile)
module = importlib.util.module_from_spec(spec)
sys.modules[module_name] = module
spec.loader.exec_module(module)

if hasattr(module,"App"):
tagClass=getattr(module,"App")

# run part (here FULL DEV)
from htag.runners import Runner
app=Runner(tagClass,reload=True,dev=True)
app.run()
else:
print("ERROR",htagfile,"doesn't contain 'App' (tag class)")
except Exception as e:
print("ERROR",e)
else:
##########################################################################
## create mode
##########################################################################
newfile = "main.py"

if not os.path.isfile(newfile):
with open(newfile,"w+") as fid:
fid.write(code)
if not os.path.isfile(newfile):
with open(newfile,"w+") as fid:
fid.write(code)

print("HTag App file created --> main.py")
else:
print(f"It seems that you've already got a {newfile} file")
print("HTag App file created --> main.py")
else:
print(f"It seems that you've already got a {newfile} file")
8 changes: 5 additions & 3 deletions htag/runners/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,9 +303,11 @@ def add_route(self,path,handler):
self._routes.append( (path, handler) )


def run(self, host="127.0.0.1", port=8000, openBrowser=True, size=(800,600)): # localhost, by default !!
# self.server = ServerHTTP(host,port,self.session, routes=self._routes, reload=self._reload, dev=self._dev, exit_callback=self.stop)
self.server = ServerWS(host,port,self.session, routes=self._routes, reload=self._reload, dev=self._dev, exit_callback=self.stop)
def run(self, host="127.0.0.1", port=8000, openBrowser=True, size=(800,600), http_only:bool=False): # localhost, by default !!
if http_only:
self.server = ServerHTTP(host,port,self.session, routes=self._routes, reload=self._reload, dev=self._dev, exit_callback=self.stop)
else:
self.server = ServerWS(host,port,self.session, routes=self._routes, reload=self._reload, dev=self._dev, exit_callback=self.stop)

loop = asyncio.get_event_loop()
server = loop.run_until_complete( self.server.run() )
Expand Down
20 changes: 20 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

# -*- coding: utf-8 -*-
# the simplest htag'app, in the best env to start development (hot reload/refresh)

from htag import Tag

class App(Tag.body):
statics="body {background:#EEE;}"

def init(self):
self += "xxx World!!!"

#=================================================================================
from htag.runners import BrowserHTTP as Runner
# from htag.runners import BrowserHTTP as Runner
# from htag.runners import ChromeApp as Runner

app=Runner(App)
if __name__=="__main__":
app.run()
2 changes: 1 addition & 1 deletion manual_tests_qp.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def init(self,param="nada"):
self <= Tag.a("test '?param=A2'",_href="?param=A2",_style="display:block")
self <= Tag.button("error", _onclick=lambda o: fdsgdfgfdsgfds())
self <= Tag.button("add content", _onclick=self.add_content) # just to control interact
self <= Tag.button("quit app", _onclick=lambda o: self.exit()) # just to test QUIT/EXIT app
self <= Tag.button("EXIT app", _onclick=lambda o: self.exit()) # just to test QUIT/EXIT app
self <= Tag.hr()

self <= Tag.h3("Only if it handles tha '/other' route (DevApp/htagweb) :")
Expand Down

0 comments on commit d22aa5d

Please sign in to comment.