Skip to content

Commit

Permalink
wip: start of exporting app as dict/json.
Browse files Browse the repository at this point in the history
  • Loading branch information
mchilvers committed Mar 22, 2024
1 parent 6e187eb commit d8c9a71
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 36 deletions.
106 changes: 81 additions & 25 deletions examples/farmyard/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,31 +171,87 @@ def make_pigs(number_of_oinks):
# Add a page that shows the code! ######################################################


app.content.append(
Page(
name="Code",
content=[
Row(
content=[
Button(
name="to_lucy",
label="Visit Lucy",
channel="navigate",
position="FILL",
),
Button(
name="to_percy",
label="Visit Percy",
channel="navigate",
position="FILL",
),
]
),
Code(code=export.as_pyscript_app(app)[0]),
],
)
)

# app.content.append(
# Page(
# name="Code",
# content=[
# Row(
# content=[
# Button(
# name="to_lucy",
# label="Visit Lucy",
# channel="navigate",
# position="FILL",
# ),
# Button(
# name="to_percy",
# label="Visit Percy",
# channel="navigate",
# position="FILL",
# ),
# ]
# ),
# #Code(code=export.as_pyscript_app(app)[0]),
# Code(code=export.as_dict(app)),
# ],
# )
# )


def to_minified_json(obj):
import zlib
import base64
import json

# Serialized JSON string
serialized_json = json.dumps(obj)
print("JSON length:", len(serialized_json))

# Convert the serialized JSON string to bytes
json_bytes = serialized_json.encode('utf-8')

# Compress the bytes using gzip
compressed_data = zlib.compress(json_bytes)#, level=zlib.Z_BEST_COMPRESSION)

# Encode the compressed data using Base64
encoded_data = base64.b64encode(compressed_data)

result = encoded_data.decode('utf-8')
print("MINIFIED length:", len(result))
return result

def from_minified_json(minified_json):
import zlib
import base64

encoded_data = minified_json#.decode('utf-8')
# Base64-encoded string
#encoded_data = "eJwzTcc0tTK3dPFSyC9LKE7MzlVLzs8FAA+A1YU="

# Decode the Base64-encoded data
decoded_data = base64.b64decode(encoded_data)

# Decompress the data using zlib
decompressed_data = zlib.decompress(decoded_data)

# Print the decompressed data
import json
return json.loads(decompressed_data.decode('utf-8'))


from pprint import pprint
app_as_dict = export.as_dict(app)
pprint(app_as_dict)

print("*"*40)
minified_json = to_minified_json(app_as_dict)
print(minified_json, len(minified_json), type(minified_json))

print("-"*40)
app_dict = from_minified_json(minified_json)
print(app_dict, type(app_dict))

print(app_dict == app_as_dict)

# GO! ##################################################################################

Expand Down
4 changes: 3 additions & 1 deletion src/invent/ui/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,7 @@ def as_dict(self):
properties = {
key: getattr(self, key) for key in type(self).properties()
}

return {
"type": type(self).__name__,
"properties": properties,
Expand Down Expand Up @@ -1067,7 +1068,8 @@ def as_dict(self):
content of children.
"""
result = super().as_dict()
result["content"] = [child.as_dict for child in self.content]
result["properties"]["content"] = [child.as_dict() for child in self.content]
return result


class Column(Container):
Expand Down
12 changes: 12 additions & 0 deletions src/invent/ui/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,18 @@ def as_pyscript_app(app, imports=IMPORTS, datastore="", code="", to_psdc=True):
return index_html, main_py, pyscript_toml


def as_dict(app, imports=IMPORTS, datastore="", code="", to_psdc=True):
"""Export an app as a dictionary."""

return dict(
imports={},
datastore={},
blocks={},
app=app.as_dict()

)


# Internal ###################################################################


Expand Down
10 changes: 0 additions & 10 deletions src/invent/ui/page.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,6 @@ class Page(Column):
widgets to achieve some aim.
"""

def as_dict(self):
"""
Return a dictionary representation of the object.
"""
return dict(
name=self.name,
id=self.id,
content=[item.as_dict() for item in self.content],
)

def render(self):
"""
Returns an HTML element to insert into the DOM.
Expand Down

0 comments on commit d8c9a71

Please sign in to comment.