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
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'>
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'>
Iterates over all the Widget subclasses in the modules that are declared as toscawidgets.widgets entrypoints
>>> len(list(all_widgets())) > 0
True
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'
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
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')
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'
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'
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.