widgetbrowser.util

class widgetbrowser.util.WidgetParameters(widget)

Wraps a widget and provides information on it’s parameters.

Example:

>>> from tw.core.base import Widget
>>> params = WidgetParameters(Widget)
>>> len(params.all) > 0
True
>>> params.all - params.local == params.inherited
True
get_default(name, default=None)

Returns a string representation of a parameter default value.

Example:

>>> from tw.core.base import Widget
>>> params = WidgetParameters(Widget)
>>> type(params.get_default('id'))
<type 'str'>
get_doc(name, default='Undocumented')

Returns parameters’s doc. If it is not documented it will return default

Example:

>>> from tw.core.base import Widget
>>> params = WidgetParameters(Widget)
>>> type(params.get_doc('id'))
<type 'str'>
widgetbrowser.util.all_widgets()

Iterates over all the Widget subclasses in the modules that are declared as toscawidgets.widgets entrypoints

>>> len(list(all_widgets())) > 0
True
widgetbrowser.util.display_widget(widget, argstr='', ctx=None)

Displays a widget with optional arguments as a string.

Warning

IT IS VERY UNSAFE TO EXECUTE UNTRUSTED CODE

Example:

>>> from tw.api import Widget
>>> class TestWidget(Widget):
...     params = ['param']
...     template = "$value-$param"
>>> display_widget(TestWidget())
u'None-None'
>>> display_widget(TestWidget(), '"hello", param=2')
u'hello-2'
widgetbrowser.util.format_code(code, format='python')

Formats a chunk of code as html. If pygments is installed it will pygmentize it.

>>> code = "from __future__ import teletransportation"
>>> '<pre' in format_code(code)
True
widgetbrowser.util.import_widget(path)

Import a widget given a dotted or entry point notation path.

Can import both widget instances or classes.

>>> import_widget('tw.core.base.Widget')
<class 'tw.core.base.Widget'>
>>> import_widget('tw.core.base:Widget')
<class 'tw.core.base.Widget'>
>>> import_widget('os.popen')
widgetbrowser.util.pretty_print(obj)
Pretty prints an object returning pygmentized code if possible.
widgetbrowser.util.widget_path(widget)

Returns the path of a widget instance or subclass

>>> from tw.api import Widget
>>> widget_path(Widget)
'tw.core.base.Widget'
>>> widget_path(Widget())
'tw.core.base.Widget'
widgetbrowser.util.widget_template(widget, extension='.html')

Returns the template of a widget as a string.

Loads external templates if needed.

>>> from widgetbrowser.tests.widgets import *
>>> widget_template(TestWidgetExternalTemplate())
'Dummy template $value\n'

If more than one is available, it will use the extension to disambiguate.

>>> from widgetbrowser.tests.widgets import TestWidgetExternalTemplate
>>> widget_template(TestWidgetMultipleExternalTemplate(), '.html')
'Dummy template $value\n'
>>> widget_template(TestWidgetMultipleExternalTemplate(), '.mak')
'Dummy template $value mako version\n'
widgetbrowser.util.widget_url(widget, action='', prefix=None)

Returns the URL of the controller to perform action on widget.

If no prefix is passed the it will be tried to be fetched from tw.framework.request_local where the WidgetBrowser leaves it on every request.

Example:

>>> from tw.core.base import Widget
>>> widget_url(Widget)
'/tw.core.base.Widget/'
>>> widget_url(Widget())
'/tw.core.base.Widget/'

You can also pass the path of the widget:

>>> widget_url('tw.api.Widget')
'/tw.api.Widget/'
>>> widget_url('tw.api.Widget', 'show')
'/tw.api.Widget/show'
>>> widget_url('tw.api.Widget', 'show', prefix='/widget')
'/widget/tw.api.Widget/show'

Note

You can pass any path the widget can be imported from when passing a string but if you pass a widget instance or subclass then the real module where it is defined will be generated.

Previous topic

widgetbrowser.repl

Next topic

widgetbrowser.widgets

This Page

Quick search