Skip to content

Commit

Permalink
works on #475 and #479
Browse files Browse the repository at this point in the history
  • Loading branch information
WolfgangFahl committed Sep 7, 2022
1 parent f37288d commit f555d76
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 10 deletions.
2 changes: 1 addition & 1 deletion justpy/htmlcomponents.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from .tailwind import Tailwind
import logging
import httpx
from .template import PageOptions

# Dictionary for translating from tag to class
_tag_class_dict = {}
Expand Down Expand Up @@ -48,7 +49,6 @@ def __call__(self, cls, **kwargs):
register_component(cls, self.tag, self.attributes)
return cls


class WebPage:
# TODO: Add page events online, beforeunload, resize
instances = {}
Expand Down
4 changes: 4 additions & 0 deletions justpy/justpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from .chartcomponents import *
from .gridcomponents import *
from .quasarcomponents import *
from .template import Context

# from .misccomponents import *
from .meadows import *
Expand Down Expand Up @@ -255,6 +256,9 @@ async def get(self, request):
"page_options": page_options,
"html": load_page.html,
}
# wrap the context in a context object to make it available
context_obj=Context(context)
context["context_obj"]=context_obj
response = templates.TemplateResponse(load_page.template_file, context)
if SESSIONS and new_cookie:
cookie_value = cookie_signer.sign(request.state.session_id)
Expand Down
40 changes: 40 additions & 0 deletions justpy/template.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'''
Created on 2022-09-07
@author: wf
'''
class Context:
"""
legacy context handler, encapsulates context
"""

def __init__(self,context_dict:dict):
self.context_dict=context_dict
self.page_options=PageOptions(context_dict.get("page_options",{}))

def as_javascript(self):
"""
generate my initial JavaScript
"""
title=self.page_options.getTitle()
debug=str(self.page_options.getDebug()).lower()
javascript=f"""let justpy_core=new JustpyCore(
this, // window
'{title}', // title
{debug} // debug
);"""
return javascript

class PageOptions:
"""
legacy page_options handler, encapsulating page_options
"""

def __init__(self,page_options_dict:dict):
self.page_options_dict=page_options_dict

def getTitle(self):
return self.page_options_dict.get("title","JustPy")

def getDebug(self):
return self.page_options_dict.get("debug",False)
3 changes: 2 additions & 1 deletion justpy/templates/js/justpy_core.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,10 @@
class JustpyCore {

// create a JustpyCore instance
constructor(window,title) {
constructor(window,title,debug) {
this.window=window
this.setTitle(title);
this.debug=debug
}

// set the title
Expand Down
12 changes: 5 additions & 7 deletions justpy/templates/main.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@
<script src='/templates/js/{{file_name}}.js'></script>
{%- endfor %}
<script>
let justp_core=new JustpyCore(
this, // window
'{{ page_options.title }}' // title
);
{%- if context_obj %}
{{ context_obj.as_javascript() | safe }}
{% endif %}
{%- for event in page_options.events %}
document.addEventListener('{{ event }}', function (evt) {
console.log(evt);
Expand Down Expand Up @@ -79,11 +78,10 @@

socket.addEventListener('message', function (event) {
msg = JSON.parse(event.data);
{% if page_options.debug %}
if (justpy_core.debug) {
console.log('Message received from server ', msg);
console.log(event);
{% endif %}

}
let e = {};
switch (msg.type) {
case 'page_update':
Expand Down
2 changes: 1 addition & 1 deletion tests/test_importability.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def test_importability(self):
assert justpy
outputText = outputBuf.getvalue()
debug = self.debug
# debug=True
#debug=True
if debug:
print(outputText)
self.assertTrue("Module directory" in outputText)
Expand Down

0 comments on commit f555d76

Please sign in to comment.