def test_initialize(self): from wdom.server import _tornado old_doc = get_document() old_app_tornado = _tornado.get_app() reset() self.assertIsNot(old_doc, get_document()) self.assertIsNot(old_app_tornado, _tornado.get_app())
def test_set_new_document(self): old_doc = get_document() doc = get_new_document() self.assertIsNot(doc, old_doc) set_document(doc) new_doc = get_document() self.assertIsNot(doc, old_doc) self.assertIs(doc, new_doc)
def sample_page(**kwargs): get_document().register_theme(default) app = Div(style='width: 90vw; margin: auto') title = H1('Simple Markdown Editor', class_='text-center') app.appendChild(title) app.appendChild(Hr()) app.appendChild(Editor()) return app
def test_initialize(self): from wdom.server import _tornado old_doc = get_document() old_app_tornado = _tornado.get_app() testing.reset() self.assertIsNot(old_doc, get_document()) self.assertIsNot(old_app_tornado, _tornado.get_app()) try: from wdom.server import _aiohttp old_app_aio = _aiohttp.get_app() testing.reset() self.assertIsNot(old_app_aio, _aiohttp.get_app()) except ImportError: pass
def sample_page(**kwargs) -> Document: app = Container() title = H1(parent=app) title.textContent = 'Todo example' form = FormGroup(parent=app) text_input = TextInput() form.append(text_input) add_button = Button(parent=form) add_button.textContent = 'ADD' todo_list = Div(parent=app) todo_heading = Div(parent=todo_list) todo_heading.append('Todo list') # add_button.addEventListener('click') def new_item(event=None) -> Tag: item = Item() item.append('New Item') todo_list.append(item) return item add_button.addEventListener('click', new_item) page = get_document(app=app, **kwargs) for css in css_files: page.add_cssfile(css) for js in js_files: page.add_jsfile(js) return page
def start_server(address: str = None, port: int = None, check_time: int = 500, **kwargs: Any) -> module.HTTPServer: """Start web server on ``address:port``. Use wrapper function :func:`start` instead. :arg str address: address of the server [default: localhost]. :arg int port: tcp port of the server [default: 8888]. :arg int check_time: millisecondes to wait until reload browser when autoreload is enabled [default: 500]. """ # Add application's static files directory from wdom.document import get_document add_static_path('_static', STATIC_DIR) doc = get_document() if os.path.exists(doc.tempdir): add_static_path('tmp', doc.tempdir, no_watch=True) if doc._autoreload or config.autoreload or config.debug: autoreload.start(check_time=check_time) global _server _server = module.start_server(address=address, port=port, **kwargs) logger.info('Start server on {0}:{1:d}'.format(server_config['address'], server_config['port'])) # start messaging loop asyncio.ensure_future(_message_loop()) if config.open_browser: open_browser( 'http://{}:{}/'.format(server_config['address'], server_config['port']), config.browser) return _server
def setUp(self): super().setUp() reset() # I don't know why, but need reset for aiohttp. from wdom.examples.timer import sample_app document = get_document() document.body.prepend(sample_app()) self.start()
def setUp(self): super().setUp() document = get_document() self.h1 = H1() self.h1.textContent = 'TITLE' document.body.appendChild(self.h1) self.start()
def main(): AsyncIOMainLoop().install() doc = get_document() if not options.config.no_default_js: doc.add_jsfile( 'https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js') doc.add_jsfile('static/bootstrap.min.js') if not options.config.no_default_css: doc.add_cssfile('static/bootstrap.min.css') _user_static_dirs = set() for js in options.config.js_files: _user_static_dirs.add(path.dirname(js)) doc.add_jsfile(js) for css in options.config.css_files: _user_static_dirs.add(path.dirname(css)) doc.add_cssfile(css) # choices arg is better, but detecting error is not easy in livemark.vim if options.config.highlight_theme in STYLE_MAP: pygments_style = HtmlFormatter( style=options.config.highlight_theme).get_style_defs() else: pygments_style = HtmlFormatter('default').get_style_defs() doc.head.appendChild(Style(pygments_style)) script = Script(parent=doc.body) script.innerHTML = ''' function moveToElement(id) { var elm = document.getElementById(id) if (elm) { var x = window.scrollX var rect = elm.getBoundingClientRect() window.scrollTo(x, rect.top + window.scrollY) console.log(elm.textContent) } } ''' mount_point = Div(parent=doc.body, class_='container') mount_point.appendChild(H2('LiveMark is running...')) app = get_app(doc) app.add_static_path('static', static_dir) for _d in _user_static_dirs: app.add_static_path(_d, _d) web_server = start_server(app, port=options.config.browser_port) loop = asyncio.get_event_loop() vim_server = Server(port=options.config.vim_port, loop=loop, doc=doc, mount_point=mount_point) browser = webbrowser.get(options.config.browser) browser.open('http://localhost:{}'.format(options.config.browser_port)) try: vim_server.start() loop.run_forever() except KeyboardInterrupt: vim_server.stop() web_server.stop()
def setUp(self): super().setUp() reset() # I don't know why, but need reset for aiohttp. document = get_document() self.h1 = H1() self.h1.textContent = 'TITLE' document.body.appendChild(self.h1) self.start()
def sample_page(theme=default) -> Document: page = get_document() app = sample_app(theme) page.body.prepend(app) for css in theme.css_files: page.add_cssfile(css) for js in theme.js_files: page.add_jsfile(js) return page
def setUp(self): self.doc = get_document() self.doc.body.appendChild(self.app()) self.server = start_server(port=0) self.addr = server_config['address'] self.port = server_config['port'] self.url = 'http://{}:{}/'.format(self.addr, self.port) self.page = page sync(self.page.goto(self.url))
async def test_tempfile(self): doc = get_document() self.assertTrue(path.exists(doc.tempdir)) tmp = path.join(doc.tempdir, 'a.html') self.assertFalse(path.exists(tmp)) with open(tmp, 'w') as f: f.write('test') self.assertTrue(path.exists(tmp)) response = await self.fetch(self.url + '/tmp/a.html') self.assertEqual(response.code, 200) self.assertEqual(response.text, 'test')
def test_tempfile(self): doc = get_document() self.assertTrue(path.exists(doc.tempdir)) tmp = path.join(doc.tempdir, 'a.html') self.assertFalse(path.exists(tmp)) with open(tmp, 'w') as f: f.write('test') self.assertTrue(path.exists(tmp)) response = yield from self.fetch(self.url + '/tmp/a.html') self.assertEqual(response.code, 200) self.assertEqual(response.text, 'test')
def setUp(self): super().setUp() self.document = get_document() class Root(Tag): tag = "root" self.root = Root() self.document.body.prepend(self.root) self.start() self.set_element(self.root)
def setUp(self): from syncer import sync super().setUp() self.doc = get_document() self.root = self.get_elements() self.doc.body.prepend(self.root) self.server = server.start_server(port=0) self.address = server_config['address'] self.port = server_config['port'] self.url = 'http://{}:{}'.format(self.address, self.port) sync(self.page.goto(self.url)) self.element = sync(self.get_element_handle(self.root))
def sample_page(**kwargs): doc = get_document(**kwargs) for js in js_files: doc.add_jsfile(js) for css in css_files: doc.add_cssfile(css) app = Div(parent=doc.body, style="width: 90vw; margin: auto") title = H1("Simple Markdown Editor", class_="text-center") app.appendChild(title) app.appendChild(Hr()) app.appendChild(Editor()) return doc
def test_add_eventlistener(self): mock = MagicMock(_is_coroutine=False) js_mock = MagicMock() doc = get_document() doc.js_exec = js_mock doc.addEventListener('click', mock) js_mock.assert_called_once_with('addEventListener', 'click') msg = { 'type': 'click', 'currentTarget': {'id': 'document'}, 'target': {'id': 'document'}, } e = event_handler(msg) mock.assert_called_once_with(e)
def setUp(self): super().setUp() self.document = get_document(autoreload=False) self.root = Div() self.select = Select(parent=self.root) self.mselect = Select(parent=self.root, multiple=True) self.opt1 = Option("option 1", parent=self.select) self.opt2 = Option("option 2", parent=self.select) self.opt3 = Option("option 3", parent=self.select, value="opt3") self.opt1m = Option("option 1", parent=self.mselect) self.opt2m = Option("option 2", parent=self.mselect) self.opt3m = Option("option 3", parent=self.mselect, value="opt3m") self.document.body.prepend(self.root) self.start()
def setUp(self): super().setUp() self.document = get_document(autoreload=False) self.root = Form() self.input = Input(parent=self.root, type="text") self.textarea = Textarea(parent=self.root) self.checkbox = CheckBox(parent=self.root, id="check1") self.check_l = Label("Check 1", parent=self.root, **{"for": "check1"}) self.radio1 = Input(parent=self.root, type="radio", name="radio_test", id="r1") # noqa self.radio2 = Input(parent=self.root, type="radio", name="radio_test", id="r2") # noqa self.radio3 = Input(parent=self.root, type="radio", name="radio_test2", id="r3") # noqa self.radio1_l = Label("Radio 1", parent=self.root, **{"for": "r1"}) self.radio2_l = Label("Radio 2", parent=self.root, **{"for": "r2"}) self.radio3_l = Label("Radio 3", parent=self.root, **{"for": "r3"}) self.document.body.prepend(self.root) self.start()
def sample_page(**kwargs): doc = get_document() win = doc.defaultView app = Div() inp = Input(id='input', parent=app) win1 = H1(id='win1', parent=app) doc1 = H1(id='doc1', parent=app) input_view = H1(id='input_view', parent=app) def add_letter_doc(e): doc1.textContent = doc1.textContent + e.key def add_letter_win(e): win1.textContent = win1.textContent + e.key def input_handler(e): input_view.textContent = e.data doc.addEventListener('keypress', add_letter_doc) win.addEventListener('keypress', add_letter_win) inp.addEventListener('input', input_handler) return app
def start_server(app: Optional[module.Application] = None, browser: Optional[str] = None, address: Optional[str] = None, check_time: Optional[int] = 500, **kwargs): """Start web server.""" # Add application's static files directory from wdom.document import get_document add_static_path('_static', static_dir) doc = get_document() if os.path.exists(doc.tempdir): add_static_path('tmp', doc.tempdir, no_watch=True) if doc._autoreload or config.autoreload or config.debug: install_asyncio() autoreload.start(check_time=check_time) global _server _server = module.start_server(**kwargs) logger.info('Start server on {0}:{1:d}'.format( _server.address, _server.port)) if config.open_browser: open_browser('http://{}:{}/'.format(_server.address, _server.port), browser or config.browser) return _server
def get(self): """Return whole html representation of the root document.""" from wdom.document import get_document logger.info('connected') self.write(get_document().build())
def test_get_element_by_wdom_id_win(self): win = getElementByWdomId('window') self.assertIs(get_document().defaultView, win)
def test_get_element_by_wdom_id_doc(self): doc = getElementByWdomId('document') self.assertIs(get_document(), doc)
def setUp(self) -> None: super().setUp() self.document = get_document() self.document.body.prepend('testing') self.start()
def setUp(self): super().setUp() self.document = get_document() self.document.body.prepend(self.get_elements()) self.start()
def setUp(self): super().setUp() from wdom.examples.data_binding import sample_app document = get_document() document.body.prepend(sample_app()) self.start()
import asyncio from wdom.document import get_document from wdom.server import start_server, stop_server from wdom.tag import Button if __name__ == '__main__': document = get_document() # Add <link>-tag sourcing bootstrap.min.css on <head> document.add_cssfile('https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css') # Add <script>-tag sourcing jquery and bootstrap.min.js to <body> document.add_jsfile('https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js') document.add_jsfile('https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js') # Add bootstrap button element document.body.appendChild(Button('click', class_='btn btn-primary')) start_server() try: asyncio.get_event_loop().run_forever() except KeyboardInterrupt: stop_server()
def setUp(self) -> None: super().setUp() self.document = get_document() self.start()
def setUp(self) -> None: super().setUp() server.add_static_path('a', path.abspath(path.dirname(__file__))) self.document = get_document() self.start()
def main_handler(request): """Main handler to serve root ``document`` object of the application.""" from wdom.document import get_document logger.info('connected') return web.Response(body=get_document().build().encode())
def get(self) -> None: """Return whole html representation of the root document.""" from wdom.document import get_document logger.info('connected') self.write(get_document().build())
def main(): AsyncIOMainLoop().install() doc = get_document() if not options.config.no_default_js: doc.add_jsfile( 'https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js' ) doc.add_jsfile('static/bootstrap.min.js') if not options.config.no_default_css: doc.add_cssfile('static/bootstrap.min.css') _user_static_dirs = set() for js in options.config.js_files: _user_static_dirs.add(path.dirname(js)) doc.add_jsfile(js) for css in options.config.css_files: _user_static_dirs.add(path.dirname(css)) doc.add_cssfile(css) # choices arg is better, but detecting error is not easy in livemark.vim if options.config.highlight_theme in STYLE_MAP: pygments_style = HtmlFormatter( style=options.config.highlight_theme).get_style_defs() else: pygments_style = HtmlFormatter('default').get_style_defs() doc.head.appendChild(Style(pygments_style)) script = Script(parent=doc.body) script.innerHTML = ''' function moveToElement(id) { var elm = document.getElementById(id) if (elm) { var x = window.scrollX var rect = elm.getBoundingClientRect() window.scrollTo(x, rect.top + window.scrollY) console.log(elm.textContent) } } ''' mount_point = Div(parent=doc.body, class_='container') mount_point.appendChild(H2('LiveMark is running...')) app = get_app(doc) app.add_static_path('static', static_dir) for _d in _user_static_dirs: app.add_static_path(_d, _d) web_server = start_server(app, port=options.config.browser_port) loop = asyncio.get_event_loop() vim_server = Server(port=options.config.vim_port, loop=loop, doc=doc, mount_point=mount_point) browser = webbrowser.get(options.config.browser) browser.open('http://localhost:{}'.format(options.config.browser_port)) try: vim_server.start() loop.run_forever() except KeyboardInterrupt: vim_server.stop() web_server.stop()
def __init__(self, name=None): super().__init__(name=name) self.document = get_document() self.document.head.appendChild(Style(style)) self.ul = Ul(class_='zen-log') self.document.body.appendChild(self.ul)
def setUp(self): super().setUp() self.doc = get_document() self.win = self.doc.defaultView self.conn_mock = MagicMock() _tornado.connections.append(self.conn_mock)
Td(str(msg.count)), Td(msg.msgType["name"]), Td(str(msg.id)), Td(str(len(msg.data))), self.contents, ) def switch_view(self, event): logger.debug("Changing view for message.") if not self.contents.textContent: self.contents.textContent = pformat(self.msg.json()) else: self.contents.textContent = "" document = get_document() document.register_theme(bootstrap3) document.add_jsfile("https://unpkg.com/sticky-table-headers") def init(start, capture_file=None): logger.debug("Initializing UI...") global ui ui = SnifferUI(start, capture_file=capture_file) set_app(ui) logger.debug("Finished initializing UI.") def loop_in_thread(loop): logger.debug("Starting loop: `{}` in thread".format(loop)) asyncio.set_event_loop(loop)