Skip to content

Commit

Permalink
improves for #498 and #479
Browse files Browse the repository at this point in the history
  • Loading branch information
WolfgangFahl committed Sep 7, 2022
1 parent e44df4c commit 8fd98f5
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 23 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/upload-to-pypi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install flit
pip install hatch
- name: Build and publish
run: |
pip install .
flit build
hatch build
- name: Publish distribution to PyPI
uses: pypa/gh-action-pypi-publish@release/v1
with:
Expand Down
22 changes: 17 additions & 5 deletions justpy/template.py → jpcore/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,26 @@ class Context:
"""

def __init__(self,context_dict:dict):
"""
constructor
Args:
context_dict(dict): a context dict in legacy format
"""
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()
title=self.page_options.get_title()
debug=str(self.page_options.get_debug()).lower()
page_ready=str(self.page_options.get_page_ready()).lower()
javascript=f"""let justpy_core=new JustpyCore(
this, // window
'{title}', // title
{page_ready}, // page_ready
{debug} // debug
);"""
return javascript
Expand All @@ -32,9 +40,13 @@ class PageOptions:

def __init__(self,page_options_dict:dict):
self.page_options_dict=page_options_dict
self.events=page_options_dict["events"]

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

def getDebug(self):
return self.page_options_dict.get("debug",False)
def get_debug(self):
return self.page_options_dict.get("debug",False)

def get_page_ready(self):
return "page_ready" in self.events
2 changes: 1 addition & 1 deletion justpy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""JustPy is an object-oriented, component based, high-level Python Web Framework that requires no front-end programming"""
from .justpy import *

__version__ = "0.6.6"
__version__ = "0.6.7"
2 changes: 1 addition & 1 deletion justpy/htmlcomponents.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from .tailwind import Tailwind
import logging
import httpx
from .template import PageOptions
from jpcore.template import PageOptions

# Dictionary for translating from tag to class
_tag_class_dict = {}
Expand Down
2 changes: 1 addition & 1 deletion justpy/justpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from .chartcomponents import *
from .gridcomponents import *
from .quasarcomponents import *
from .template import Context
from jpcore.template import Context

# from .misccomponents import *
from .meadows import *
Expand Down
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,debug) {
constructor(window,title,page_ready,debug) {
this.window=window
this.setTitle(title);
this.page_ready=page_ready
this.debug=debug
}

Expand Down
19 changes: 9 additions & 10 deletions justpy/templates/main.html
Original file line number Diff line number Diff line change
Expand Up @@ -116,16 +116,15 @@
case 'websocket_update':
websocket_id = msg.data;
websocket_ready = true;

{% if 'page_ready' in page_options.events %}
e = {
'event_type': 'page_ready',
'visibility': document.visibilityState,
'page_id': page_id,
'websocket_id': websocket_id
};
send_to_server(e, 'page_event', false);
{% endif %}
if (justpy_core.page_ready) {
e = {
'event_type': 'page_ready',
'visibility': document.visibilityState,
'page_id': page_id,
'websocket_id': websocket_id
};
send_to_server(e, 'page_event', false);
}
break;
case 'component_update':
// update just specific component on the page
Expand Down
13 changes: 11 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# see https://flit.pypa.io/en/latest/pyproject_toml.html
[build-system]
requires = ["flit_core >=3.2,<4"]
build-backend = "flit_core.buildapi"
#requires = ["flit_core >=3.2,<4"]
#build-backend = "flit_core.buildapi"
requires = ["hatchling"]
build-backend = "hatchling.build"

[project]
name = "justpy"
Expand Down Expand Up @@ -47,6 +49,8 @@ classifiers=[
"License :: OSI Approved :: Apache Software License"
]
dynamic = ["version", "description"]
[tool.hatch.version]
path = "justpy/__init__.py"

[project.urls]
Home = "https://justpy.io/"
Expand All @@ -67,3 +71,8 @@ test = [
"webdriver-manager"
]

[tool.hatch.build.targets.wheel]
packages = [
"jpcore",
"justpy",
]
65 changes: 65 additions & 0 deletions tests/test_template.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
'''
Created on 2022-09-07
@author: wf
'''
from tests.basetest import Basetest
from jpcore.template import Context


class TestTemplate(Basetest):
"""
Tests template handling
"""

def test_javascript(self):
"""
test javascript generation
"""
context_dict={'html': '',
'justpy_dict': '[{"attrs": {}, "id": null, "vue_type": "html_component", '
'"show": true, "events": [], "event_modifiers": {}, "classes": '
'"text-5xl m-2", "style": "", "set_focus": false, "html_tag": '
'"p", "class_name": "P", "event_propagation": true, '
'"inner_html": "", "animation": false, "debug": false, '
'"transition": null, "directives": {}, "scoped_slots": {}, '
'"object_props": [], "text": "Hello there!"}]',
'options': {'aggrid': True,
'aggrid_enterprise': False,
'bokeh': False,
'component_file_list': [],
'deckgl': False,
'highcharts': True,
'katex': False,
'no_internet': True,
'plotly': False,
'quasar': False,
'quasar_version': None,
'static_name': 'static',
'tailwind': True,
'vega': False},
'page_id': 0,
'page_options': {'body_classes': '',
'body_html': '',
'body_style': '',
'css': '',
'dark': False,
'debug': False,
'display_url': None,
'events': [],
'favicon': '',
'head_html': '',
'highcharts_theme': None,
'redirect': None,
'reload_interval': None,
'title': 'JustPy'},
'request': None,
'use_websockets': 'true'}
context_obj=Context(context_dict)
js=context_obj.as_javascript()
debug=True
if debug:
print(js)
for param in ["window","title","page_ready","debug"]:
self.assertTrue(f"// {param}" in js)

0 comments on commit 8fd98f5

Please sign in to comment.