From d22aa5d2ef64a10a5777e5bb2183061fe79c1342 Mon Sep 17 00:00:00 2001 From: manatlan Date: Sun, 25 Feb 2024 10:13:29 +0100 Subject: [PATCH] __main__ can launch in debug mode --- htag/__main__.py | 66 +++++++++++++++++++++++++----------------- htag/runners/runner.py | 8 +++-- main.py | 20 +++++++++++++ manual_tests_qp.py | 2 +- 4 files changed, 65 insertions(+), 31 deletions(-) create mode 100644 main.py diff --git a/htag/__main__.py b/htag/__main__.py index 1e991a5..816b2d5 100644 --- a/htag/__main__.py +++ b/htag/__main__.py @@ -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 -*- @@ -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") diff --git a/htag/runners/runner.py b/htag/runners/runner.py index cbdb6a6..6f16ef6 100644 --- a/htag/runners/runner.py +++ b/htag/runners/runner.py @@ -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() ) diff --git a/main.py b/main.py new file mode 100644 index 0000000..3e13a6f --- /dev/null +++ b/main.py @@ -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() diff --git a/manual_tests_qp.py b/manual_tests_qp.py index 4b160e9..f29e336 100644 --- a/manual_tests_qp.py +++ b/manual_tests_qp.py @@ -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) :")