Ejemplo n.º 1
0
def create_ui_example(filename, to_root, height=300):
    """ Given a filename, export the containing app to HTML, return
    generated HTML. Needs to be done via filename, not direct code, so
    that PyScript can obtain source.
    """
    code = open(filename, 'rb').read().decode()
    fname = os.path.split(filename)[1]
    filename_parts = 'ui', 'examples', fname[:-3] + '.html'
    filename_abs = os.path.join(HTML_DIR, *filename_parts)
    filename_rel = to_root + '/' + '/'.join(filename_parts)
    
    # Import
    try:
        if sys.version_info >= (3, 5):
            spec = importlib.util.spec_from_file_location("example", filename)
            m = importlib.util.module_from_spec(spec)
            spec.loader.exec_module(m)
        else:  # http://stackoverflow.com/a/67692/2271927
            from importlib.machinery import SourceFileLoader
            m = SourceFileLoader("example", filename).load_module()
    except Exception as err:
        err_text = str(err)
        msg = 'Example not generated. <pre>%s</pre>' % err_text
        if os.environ.get('READTHEDOCS', False):
            msg = 'This example is not build on read-the-docs. <pre>%s</pre>' % err_text
        open(filename_abs, 'wt', encoding='utf-8').write(msg)
        warnings.warn('Could not import ui example: %s' % err_text)
        return get_html(filename_rel, 60)
    
    # Get class name
    line1 = code.splitlines()[0]
    class_name = None
    if 'class App' in code:
        class_name = 'App'
    elif 'class MyApp' in code:
        class_name = 'MyApp'
    elif 'class Example' in code:
        class_name = 'Example'
    elif line1.startswith('# doc-export:'):
        class_name = line1.split(':', 1)[1].strip()
    #
    if class_name:
        assert class_name.isidentifier()
    else:
        msg = 'Could not determine app widget class in:<pre>%s</pre>' % code
        warnings.warn(msg)
        open(filename_abs, 'wt', encoding='utf-8').write(msg)
        return get_html(filename_rel, height)
    
    # Export
    try:
        app.export(m.__dict__[class_name], filename_abs, False)
    except Exception as err:
        err_text = str(err)
        msg = 'Example not generated. <pre>%s</pre>' % err_text
        open(filename_abs, 'wt', encoding='utf-8').write(msg.replace('\\n', '<br />'))
        raise RuntimeError('Could not export ui example: %s\n%s' % (err_text, code) )
        #print('Could not create ui example: %s\n%s' % (err_text, code) )
    
    return get_html(filename_rel, height)
Ejemplo n.º 2
0
def create_ui_example(filename, to_root, height=300):
    """ Given a filename, export the containing app to HTML, return
    generated HTML. Needs to be done via filename, not direct code, so
    that PyScript can obtain source.
    """
    code = open(filename, 'rb').read().decode()
    fname = os.path.split(filename)[1]
    filename_parts = 'ui', 'examples', fname[:-3] + '.html'
    filename_abs = os.path.join(HTML_DIR, *filename_parts)
    filename_rel = to_root + '/' + '/'.join(filename_parts)

    # Import
    try:
        spec = importlib.util.spec_from_file_location("example", filename)
        m = importlib.util.module_from_spec(spec)
        spec.loader.exec_module(m)
    except Exception as err:
        err_text = str(err)
        msg = 'Example not generated. <pre>%s</pre>' % err_text
        if os.environ.get('READTHEDOCS', False):
            msg = 'This example is not build on read-the-docs. <pre>%s</pre>' % err_text
        open(filename_abs, 'wt', encoding='utf-8').write(msg)
        warnings.warn('Could not import ui example: %s' % err_text)
        return get_html(filename_rel, 60)

    # Get class name
    line1 = code.splitlines()[0]
    class_name = None
    if 'class App' in code:
        class_name = 'App'
    elif 'class MyApp' in code:
        class_name = 'MyApp'
    elif 'class Example' in code:
        class_name = 'Example'
    elif line1.startswith('# doc-export:'):
        class_name = line1.split(':', 1)[1].strip()
    #
    if class_name:
        assert class_name.isidentifier()
    else:
        msg = 'Could not determine app widget class in:<pre>%s</pre>' % code
        warnings.warn(msg)
        open(filename_abs, 'wt', encoding='utf-8').write(msg)
        return get_html(filename_rel, height)

    # Export
    try:
        app.export(m.__dict__[class_name], filename_abs, False)
    except Exception as err:
        err_text = str(err)
        msg = 'Example not generated. <pre>%s</pre>' % err_text
        open(filename_abs, 'wt',
             encoding='utf-8').write(msg.replace('\\n', '<br />'))
        raise RuntimeError('Could not export ui example: %s\n%s' %
                           (err_text, code))
        #print('Could not create ui example: %s\n%s' % (err_text, code) )

    return get_html(filename_rel, height)
Ejemplo n.º 3
0
def main():
    if len(sys.argv) > 1:
        app.export(BeetMe, "beetme.html", link=0)
    else:
        app.serve(BeetMe)
        tornado_app = app.current_server().app
        tornado_app.add_handlers(r".*", [
            (r"/beet.*", BeetHandler),
        ])
        app.start()
Ejemplo n.º 4
0
def create_ui_example(filename, to_root, height=300, source=None):
    """ Given a filename, export the containing app to HTML, return
    generated HTML. Needs to be done via filename, not direct code, so
    that PScript can obtain source.
    """
    code = open(filename, 'rb').read().decode()
    fname = os.path.split(filename)[1]
    filename_parts = 'examples', fname[:-3] + '.html'
    filename_abs = os.path.join(HTML_DIR, *filename_parts)
    filename_rel = to_root + '/' + '/'.join(filename_parts)

    # Import - mod_name must be unique, because JS modules match Py modules
    try:
        mod_name = "app_" + fname[:-3]
        if sys.version_info >= (3, 5):
            spec = importlib.util.spec_from_file_location(mod_name, filename)
            m = importlib.util.module_from_spec(spec)
            sys.modules[
                mod_name] = m  # Flexx needs to be able to access the module
            spec.loader.exec_module(m)
        else:  # http://stackoverflow.com/a/67692/2271927
            from importlib.machinery import SourceFileLoader
            m = SourceFileLoader(mod_name, filename).load_module()
            sys.modules[mod_name] = m
    except Exception as err:
        err_text = str(err)
        msg = 'Example not generated. <pre>%s</pre>' % err_text
        if os.environ.get('READTHEDOCS', False):
            msg = 'This example is not build on read-the-docs. <pre>%s</pre>' % err_text
        open(filename_abs, 'wt', encoding='utf-8').write(msg)
        warnings.warn('Could not import ui example in %s: %s' %
                      (source or filename, err_text))
        return get_html(filename_rel, 60)

    # Get class name
    line1 = code.splitlines()[0]
    class_name = None
    if 'class App(' in code:
        class_name = 'App'
    elif 'class MyApp' in code:
        class_name = 'MyApp'
    elif 'class Example' in code:
        class_name = 'Example'
    elif line1.startswith('# doc-export:'):
        class_name = line1.split(':', 1)[1].strip()
    #
    if class_name:
        assert class_name.isidentifier()
    else:
        msg = 'Could not determine app widget class in:<pre>%s</pre>' % code
        warnings.warn(msg)
        open(filename_abs, 'wt', encoding='utf-8').write(msg)
        return get_html(filename_rel, height)

    # Export
    try:
        app.export(m.__dict__[class_name],
                   filename_abs,
                   link=2,
                   overwrite=False)
    except Exception as err:
        err_text = str(err)
        msg = 'Example not generated. <pre>%s</pre>' % err_text
        open(filename_abs, 'wt',
             encoding='utf-8').write(msg.replace('\\n', '<br />'))
        print('==========\n%s\n==========' % code)
        print('ERROR: Could not export ui example: %s in %s\nSee code above.' %
              (err_text, fname))
        raise err

    all_examples.append((class_name, mod_name, filename_parts[-1]))
    return get_html(filename_rel, height)
Ejemplo n.º 5
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2018/1/3 14:26
# @Author  : yc
# @Site    :
# @File    : 01_01.py
# @Software: PyCharm

from flexx import app, ui


class Example(ui.Widget):
    CSS = '.flx-Example {background:blue; min-width: 20px; min-height:20px}'

    # def init(self):
    #     with ui.HBox():
    #         ui.Button(flex = 3,text = "hello")
    #         ui.Button(flex = 9,text = "world")

    def init(self):
        ui.Label(text="hello world")


# app.launch(Example)
# app.run()

example = app.launch(Example)
app.export(Example, 'example.html')
Ejemplo n.º 6
0
def create_ui_example(filename, to_root, height=300, source=None):
    """ Given a filename, export the containing app to HTML, return
    generated HTML. Needs to be done via filename, not direct code, so
    that PScript can obtain source.
    """
    code = open(filename, 'rb').read().decode()
    fname = os.path.split(filename)[1]
    filename_parts = 'examples', fname[:-3] + '.html'
    filename_abs = os.path.join(HTML_DIR, *filename_parts)
    filename_rel = to_root + '/' + '/'.join(filename_parts)
    
    # Import - mod_name must be unique, because JS modules match Py modules
    try:
        mod_name = "app_" + fname[:-3]
        if sys.version_info >= (3, 5):
            spec = importlib.util.spec_from_file_location(mod_name, filename)
            m = importlib.util.module_from_spec(spec)
            sys.modules[mod_name] = m  # Flexx needs to be able to access the module
            spec.loader.exec_module(m)
        else:  # http://stackoverflow.com/a/67692/2271927
            from importlib.machinery import SourceFileLoader
            m = SourceFileLoader(mod_name, filename).load_module()
            sys.modules[mod_name] = m
    except Exception as err:
        err_text = str(err)
        msg = 'Example not generated. <pre>%s</pre>' % err_text
        if os.environ.get('READTHEDOCS', False):
            msg = 'This example is not build on read-the-docs. <pre>%s</pre>' % err_text
        open(filename_abs, 'wt', encoding='utf-8').write(msg)
        warnings.warn('Could not import ui example in %s: %s' % (source or filename, err_text))
        return get_html(filename_rel, 60)
    
    # Get class name
    line1 = code.splitlines()[0]
    class_name = None
    if 'class App(' in code:
        class_name = 'App'
    elif 'class MyApp' in code:
        class_name = 'MyApp'
    elif 'class Example' in code:
        class_name = 'Example'
    elif line1.startswith('# doc-export:'):
        class_name = line1.split(':', 1)[1].strip()
    #
    if class_name:
        assert class_name.isidentifier()
    else:
        msg = 'Could not determine app widget class in:<pre>%s</pre>' % code
        warnings.warn(msg)
        open(filename_abs, 'wt', encoding='utf-8').write(msg)
        return get_html(filename_rel, height)
    
    # Export
    try:
        app.export(m.__dict__[class_name], filename_abs, link=2, overwrite=False)
    except Exception as err:
        err_text = str(err)
        msg = 'Example not generated. <pre>%s</pre>' % err_text
        open(filename_abs, 'wt', encoding='utf-8').write(msg.replace('\\n', '<br />'))
        print('==========\n%s\n==========' % code)
        print('ERROR: Could not export ui example: %s in %s\nSee code above.' % (err_text, fname))
        raise err
    
    all_examples.append((class_name, mod_name, filename_parts[-1]))
    return get_html(filename_rel, height)