Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
wang0618 committed Jun 29, 2022
1 parent 1a80a70 commit b55a6a4
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 11 deletions.
75 changes: 75 additions & 0 deletions demos/note_app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
from pywebio import start_server

from pywebio.output import *
from pywebio.input import *
from pywebio.pin import *
from pywebio.session import set_env, download, hold
import os
import os.path as op
from functools import partial

NOTE_HOME = op.join(op.expanduser("~"), 'pywebio-notes-app')


@page(silent_quit=True)
def note_editor(file):
set_env(title=file)
if not op.exists(op.join(NOTE_HOME, file)):
open(op.join(NOTE_HOME, file), 'w')

with open(op.join(NOTE_HOME, file), 'r+') as f:
md = f.read()
put_textarea('md_text', rows=18, code={'mode': 'markdown'}, value=md)

put_markdown('## Preview')
with use_scope('md', clear=True):
put_markdown(md)

while True:
change_detail = pin_wait_change('md_text')
with use_scope('md', clear=True):
f.truncate(0) # empty the file
f.write(change_detail['value'])
put_markdown(change_detail['value'], sanitize=False)


@page()
def note_viewer(file):
set_env(title=file)

md = open(op.join(NOTE_HOME, file)).read()
put_markdown(md)

put_button("Edit", lambda: note_editor(file))
hold()


def main():
"""PyWebIO Markdown Notes Application"""
set_env(output_animation=False)
files = [
f
for f in os.listdir(NOTE_HOME)
if op.isfile(op.join(NOTE_HOME, f)) and f.endswith('.md')
]

put_button("New Note", lambda: note_editor(input("Note name")))

if not files:
put_info("No existing notes found")
else:
put_table([
(
idx,
put_button(name, partial(note_viewer, name), link_style=True),
put_buttons(['Edit', 'Delete'], [partial(note_editor, name), None])
)
for idx, name in enumerate(files, start=1)
])


if __name__ == '__main__':
if not op.exists(NOTE_HOME):
os.makedirs(NOTE_HOME, exist_ok=True)

start_server(main, port=8080, debug=True, cdn=False)
23 changes: 18 additions & 5 deletions webiojs/src/models/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,28 @@ function on_page_lost(page_id: string) {
let clean_up_task_id: number = null;

export function OpenPage(page_id: string, task_id: string) {
let page = window.open(window.location.href);
if(!SubPageSession.is_sub_page(window))
AddPage(page, page_id, task_id)
else
// @ts-ignore
window._pywebio_add_page(page, page_id, task_id)
}

export function AddPage(page: Window, page_id: string, task_id: string) {
if (page_id in subpages)
throw `Can't open page, the page id "${page_id}" is duplicated`;

if (!clean_up_task_id)
clean_up_task_id = start_clean_up_task()

let page = window.open(window.location.href);
subpages[page_id] = {page: page, task_id: task_id}

// the `_pywebio_page` will be resolved in new opened page in `SubPageSession.start_session()`
// @ts-ignore
page._pywebio_page = new LazyPromise()
page._pywebio_page = new LazyPromise();
// @ts-ignore
page._pywebio_master_session = state.CurrentSession;
// @ts-ignore
page._pywebio_add_page = AddPage;


// this event is not reliably fired by browsers
// https://developer.mozilla.org/en-US/docs/Web/API/Window/pagehide_event#usage_notes
Expand All @@ -62,6 +72,9 @@ export function OpenPage(page_id: string, task_id: string) {
on_page_lost(page_id)
}, 100)
});

if (!clean_up_task_id)
clean_up_task_id = start_clean_up_task()
}

export function ClosePage(page_id: string) {
Expand Down
14 changes: 8 additions & 6 deletions webiojs/src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,13 @@ export class SubPageSession implements Session {

// check if the window is a pywebio subpage
static is_sub_page(window_obj: Window = window): boolean {
// - `window._pywebio_page` lazy promise is not undefined
// - window.opener is not null and window.opener.WebIO is not undefined
// - `window._pywebio_page` lazy promise is defined
// - window._pywebio_master_session is defined

try {
// @ts-ignore
return window_obj._pywebio_page !== undefined && window_obj.opener !== null && window_obj.opener.WebIO !== undefined;
}catch (e) {
return window_obj._pywebio_page !== undefined && window_obj._pywebio_master_session !== undefined;
} catch (e) {
return false;
}
}
Expand Down Expand Up @@ -107,12 +107,14 @@ export class SubPageSession implements Session {

// send text message to opener
send_message(msg: ClientEvent, onprogress?: (loaded: number, total: number) => void): void {
window.opener.WebIO._state.CurrentSession.send_message(msg, onprogress);
// @ts-ignore
window._pywebio_master_session.send_message(msg, onprogress);
}

// send binary message to opener
send_buffer(data: Blob, onprogress?: (loaded: number, total: number) => void): void {
window.opener.WebIO._state.CurrentSession.send_buffer(data, onprogress);
// @ts-ignore
window._pywebio_master_session.send_buffer(data, onprogress);
}

close_session(): void {
Expand Down

0 comments on commit b55a6a4

Please sign in to comment.