Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
ramedina86 committed Jan 25, 2024
2 parents 8f3d6eb + 52abe0d commit 58a9fe9
Show file tree
Hide file tree
Showing 42 changed files with 2,108 additions and 348 deletions.
30 changes: 28 additions & 2 deletions apps/hello/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ def update(state, session):
main_df = _get_main_df()
main_df = main_df[main_df['length_cm'] >= state["filter"]["min_length"]]
main_df = main_df[main_df['weight_g'] >= state["filter"]["min_weight"]]
state["main_df"] = main_df
state['main_df'] = main_df

paginated_members = _get_paginated_members(state['paginated_members_page'] - 1, state['paginated_members_page_size'])
state['paginated_members'] = paginated_members
state["session"] = session
_update_metrics(state)
_update_role_chart(state)
Expand All @@ -33,6 +36,22 @@ def handle_story_download(state):
state.file_download(data, file_name)


def handle_paginated_members_page_change(state, payload):
page = payload
maxpage = int(state["paginated_members_total_items"] / state["paginated_members_page_size"]) + 1
if page > maxpage:
state["paginated_members_page"] = maxpage - 2
else:
state["paginated_members_page"] = page

update(state, None)


def handle_paginated_members_page_size_change(state, payload):
state['paginated_members_page_size'] = payload
update(state, None)


# LOAD / GENERATE DATA


Expand All @@ -47,12 +66,15 @@ def _get_main_df():
main_df = pd.read_csv("assets/main_df.csv")
return main_df


def _get_highlighted_members():
sample_df = _get_main_df().sample(3).set_index("name", drop=False)
sample = sample_df.to_dict("index")
return sample

def _get_paginated_members(offset: int, limit: int):
paginated_df = _get_main_df()[offset:offset + limit].set_index("name", drop=False)
paginated = paginated_df.to_dict("index")
return paginated

def _get_story_text():
with open("assets/story.txt", "r") as f:
Expand Down Expand Up @@ -117,6 +139,10 @@ def _update_scatter_chart(state):
"highlighted_members": _get_highlighted_members(),
"random_df": _generate_random_df(),
"hue_rotation": 26,
"paginated_members": _get_paginated_members(0, 2),
"paginated_members_page": 1,
"paginated_members_total_items": len(_get_main_df()),
"paginated_members_page_size": 2,
"story": {
"text": _get_story_text(), # For display
},
Expand Down
74 changes: 69 additions & 5 deletions apps/hello/ui.json
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@
"width": "1",
"isCollapsible": "",
"startCollapsed": "",
"horizontalAlignment": "center"
"contentHAlign": "center"
},
"parentId": "fb22acfc-cdb5-44b6-9e97-76c3a51a8fff",
"position": 2
Expand Down Expand Up @@ -351,7 +351,7 @@
"name": "Timer"
},
"parentId": "ee919cd6-8153-4f34-8c6a-bfc1153df360",
"position": 3
"position": 4
},
"db4c66d6-1eb7-44d3-a2d4-65d0b3e5cf12": {
"id": "db4c66d6-1eb7-44d3-a2d4-65d0b3e5cf12",
Expand All @@ -367,7 +367,7 @@
"id": "09ddb2da-6fa3-4157-8da3-4d5d44a6a58d",
"type": "horizontalstack",
"content": {
"alignment": "left"
"contentHAlign": "start"
},
"parentId": "85120b55-69c6-4b50-853a-bbbf73ff8121",
"position": 0
Expand Down Expand Up @@ -591,7 +591,7 @@
"content": {
"width": "1",
"verticalAlignment": "",
"horizontalAlignment": ""
"contentHAlign": ""
},
"parentId": "b9cb10e5-1ead-448b-afcc-909e23afb72a",
"position": 0
Expand Down Expand Up @@ -843,7 +843,7 @@
"id": "9bb8a686-7013-4af7-a89e-d89c7754120d",
"type": "horizontalstack",
"content": {
"alignment": "left"
"contentHAlign": "start"
},
"parentId": "771dc336-69b2-400e-9ea3-e881e2332c9d",
"position": 2,
Expand Down Expand Up @@ -911,6 +911,70 @@
"click": "handle_story_download"
},
"visible": true
},
"e1ax8ctt8lrao0e4": {
"id": "e1ax8ctt8lrao0e4",
"type": "tab",
"content": {
"name": "Pagination"
},
"parentId": "ee919cd6-8153-4f34-8c6a-bfc1153df360",
"position": 3,
"handlers": {},
"visible": true
},
"j3jkho6tb97u0onr": {
"id": "j3jkho6tb97u0onr",
"type": "repeater",
"content": {
"keyVariable": "itemId",
"valueVariable": "item",
"repeaterObject": "@{paginated_members}"
},
"parentId": "e1ax8ctt8lrao0e4",
"position": 0,
"handlers": {},
"visible": true
},
"4wzaubf275w17gac": {
"id": "4wzaubf275w17gac",
"type": "section",
"content": {
"title": "@{item.name} \u2b50\ufe0f"
},
"parentId": "j3jkho6tb97u0onr",
"position": 0,
"handlers": {},
"visible": true
},
"19binb4yi70gesho": {
"id": "19binb4yi70gesho",
"type": "text",
"content": {
"text": "**Role:** @{item.role}\n",
"useMarkdown": "yes"
},
"parentId": "4wzaubf275w17gac",
"position": 0
},
"zfp1koasiuleygmz": {
"id": "zfp1koasiuleygmz",
"type": "pagination",
"content": {
"page": "@{paginated_members_page}",
"pageSize": "@{paginated_members_page_size}",
"totalItems": "@{paginated_members_total_items}",
"pageSizeOptions": "1,2,5",
"pageSizeShowAll": "no",
"jumpTo": "no"
},
"parentId": "e1ax8ctt8lrao0e4",
"position": 1,
"handlers": {
"ss-change-page": "handle_paginated_members_page_change",
"ss-change-page-size": "handle_paginated_members_page_size_change"
},
"visible": true
}
}
}
14 changes: 14 additions & 0 deletions docs/docs/frontend-scripts.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,20 @@ initial_state.import_script("my_script", "/static/script.js")
Importing scripts is useful to import libraries that don't support ES6 modules. When possible, use ES6 modules. The `import_script` syntax is only used for side effects; you'll only be able to call functions from the backend using modules that have been previously imported via `import_frontend_module`.
:::

## Importing a script or stylesheet from a URL

Streamsync can also import scripts and stylesheets from URLs. This is useful for importing libraries from CDNs. The `import_script` and `import_stylesheet` methods take a `url` argument, which is the URL to the script or stylesheet.

```python
initial_state = ss.init_state({
"my_app": {
"title": "My App"
},
})

initial_state.import_script("lodash", "https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.js")
```

## Frontend core

You can access Streamsync's frontend core via `globalThis.core`, unlocking all sorts of functionality. Notably, you can use `getUserState()` to get values from state.
Expand Down
2 changes: 1 addition & 1 deletion src/streamsync/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from streamsync.core import Readable, FileWrapper, BytesWrapper, Config
from streamsync.core import initial_state, component_manager, session_manager, session_verifier

VERSION = "0.2.8"
VERSION = "0.3.0"

component_manager
session_manager
Expand Down
6 changes: 4 additions & 2 deletions src/streamsync/app_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import watchdog.events
from streamsync import VERSION

logging.basicConfig(level=logging.INFO, format='%(message)s')


class MessageHandlingException(Exception):
pass
Expand Down Expand Up @@ -631,7 +633,7 @@ async def dispatch_message(self, session_id: Optional[str], request: AppProcessS
def _load_persisted_script(self) -> str:
try:
contents = None
with open(os.path.join(self.app_path, "main.py"), "r") as f:
with open(os.path.join(self.app_path, "main.py"), "r", encoding='utf-8') as f:
contents = f.read()
return contents
except FileNotFoundError:
Expand Down Expand Up @@ -799,4 +801,4 @@ async def notify_of_code_update(self):
try:
self.code_update_condition.notify_all()
finally:
self.code_update_condition.release()
self.code_update_condition.release()
30 changes: 28 additions & 2 deletions src/streamsync/app_templates/hello/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ def update(state, session):
main_df = _get_main_df()
main_df = main_df[main_df['length_cm'] >= state["filter"]["min_length"]]
main_df = main_df[main_df['weight_g'] >= state["filter"]["min_weight"]]
state["main_df"] = main_df
state['main_df'] = main_df

paginated_members = _get_paginated_members(state['paginated_members_page'] - 1, state['paginated_members_page_size'])
state['paginated_members'] = paginated_members
state["session"] = session
_update_metrics(state)
_update_role_chart(state)
Expand All @@ -33,6 +36,22 @@ def handle_story_download(state):
state.file_download(data, file_name)


def handle_paginated_members_page_change(state, payload):
page = payload
maxpage = int(state["paginated_members_total_items"] / state["paginated_members_page_size"]) + 1
if page > maxpage:
state["paginated_members_page"] = maxpage - 2
else:
state["paginated_members_page"] = page

update(state, None)


def handle_paginated_members_page_size_change(state, payload):
state['paginated_members_page_size'] = payload
update(state, None)


# LOAD / GENERATE DATA


Expand All @@ -47,12 +66,15 @@ def _get_main_df():
main_df = pd.read_csv("assets/main_df.csv")
return main_df


def _get_highlighted_members():
sample_df = _get_main_df().sample(3).set_index("name", drop=False)
sample = sample_df.to_dict("index")
return sample

def _get_paginated_members(offset: int, limit: int):
paginated_df = _get_main_df()[offset:offset + limit].set_index("name", drop=False)
paginated = paginated_df.to_dict("index")
return paginated

def _get_story_text():
with open("assets/story.txt", "r") as f:
Expand Down Expand Up @@ -117,6 +139,10 @@ def _update_scatter_chart(state):
"highlighted_members": _get_highlighted_members(),
"random_df": _generate_random_df(),
"hue_rotation": 26,
"paginated_members": _get_paginated_members(0, 2),
"paginated_members_page": 1,
"paginated_members_total_items": len(_get_main_df()),
"paginated_members_page_size": 2,
"story": {
"text": _get_story_text(), # For display
},
Expand Down
Loading

0 comments on commit 58a9fe9

Please sign in to comment.