xnano.widgets
xnano.widgets
¶
xnano.widgets
Widgets that draw themselves: text, tables, charts, gauges, figures and lists.
A widget is a description of something to draw. Pass one to Screen.write
or draw it into a Buffer; every number and text in it can be an
Expression that is evaluated again on each frame.
Attributes:
-
Bar–One labelled value in a
BarChart. -
BarChart–Bars, or groups of bars, drawn against a common scale.
-
BarGroup–A labelled group of
Barvalues drawn together in a -
Block–A border with titles and padding that frames other content.
-
Calendar–A monthly calendar whose days can be marked with styles.
-
Chart–Lines or points plotted against two axes.
-
ChartAxis–One axis of a
Chart: its bounds, labels and title. -
ChartDataset–A series of
(x, y)points that aChartdraws, with a -
Figure–A coordinate plane that shapes, points and labels are drawn on; it is not a
-
FigureCircle–A circle outline on a
Figure. -
FigureLabel–Text printed at a position of a
Figure, always on top of its -
FigureLine–A straight line between two points of a
Figure, optionally -
FigureMap–The map of the world on a
Figure, in longitude and latitude. -
FigurePoints–A set of single points on a
Figure. -
FigureRectangle–An axis-aligned rectangle outline on a
Figure. -
Gauge–A bar filled to a ratio, with an optional label.
-
ItemList–A list of items, one of which can be selected.
-
LineGauge–A progress indicator drawn with a thin line.
-
ListItem–One entry of an
ItemList, with its own style. -
Paragraph–Styled text that wraps, aligns and scrolls.
-
Scrollbar–A bar that shows where the visible part sits within longer content.
-
Sparkline–A compact history of numbers drawn as small bars.
-
SparklineBar–One sample of a
Sparklinewith a style of its own. -
Table–Rows and columns of text with an optional header and a selection.
-
TableCell–One cell of a
TableRow, with its own style and a column -
TableRow–One row of a
Table, with a height and margins. -
Tabs–A row of titles, one of which is selected.
-
Widget–The base class of every widget: a buffer painted from settings that can be
Bar
module-attribute
¶
Bar = _CoreBar
One labelled value in a BarChart.
Build bars when each value needs its own label or style.
import xnano
from xnano.widgets import Bar, BarChart
chart = BarChart([Bar(3, 'a'), Bar(5, 'b')], bar_width=3, width=8, height=4)
with xnano.new(width=8, height=4) as canvas:
canvas.screen.write(chart)
assert 'a' in canvas.screen.render().splitlines()[-1]
BarChart
module-attribute
¶
BarChart = _CoreBarChart
Bars, or groups of bars, drawn against a common scale.
Reach for it to compare a handful of values side by side.
import xnano
from xnano.widgets import Bar, BarChart
chart = BarChart([Bar(3, 'a'), Bar(5, 'b')], bar_width=3, width=8, height=4)
chart.maximum = 6
with xnano.new(width=8, height=4) as canvas:
canvas.screen.write(chart)
assert chart.maximum == 6
assert canvas.screen.render().strip()
BarGroup
module-attribute
¶
BarGroup = _CoreBarGroup
Block
module-attribute
¶
Block = _CoreBlock
A border with titles and padding that frames other content.
Pass it as the block of a widget to frame it, or write it on its own for a plain
box.
import xnano
from xnano.widgets import Block
with xnano.new(width=12, height=3) as canvas:
canvas.screen.write(Block('Status', width=12, height=3))
assert 'Status' in canvas.screen.render()
Calendar
module-attribute
¶
Calendar = _CoreCalendar
A monthly calendar whose days can be marked with styles.
Use it to show a month, or to highlight the days that hold events.
Chart
module-attribute
¶
Chart = _CoreChart
Lines or points plotted against two axes.
Reach for it to plot series of numbers; use a Figure to draw
free-form shapes instead.
import xnano
from xnano.widgets import Chart, ChartAxis, ChartDataset
series = ChartDataset(name='load', data=[(0, 0), (1, 2), (2, 1)], graph_type='line')
chart = Chart(
[series],
x_axis=ChartAxis(bounds=(0, 2)),
y_axis=ChartAxis(bounds=(0, 2)),
width=20,
height=8,
)
with xnano.new(width=20, height=8) as canvas:
canvas.screen.write(chart)
assert canvas.screen.render().strip()
ChartAxis
module-attribute
¶
ChartAxis = _CoreChartAxis
One axis of a Chart: its bounds, labels and title.
Pass two of them as x_axis and y_axis to set the range a chart shows.
import xnano
from xnano.widgets import Chart, ChartAxis, ChartDataset
axis = ChartAxis(title='time', bounds=(0, 10), labels=['0', '10'])
chart = Chart([ChartDataset(data=[(0, 0), (10, 5)])], x_axis=axis, width=24, height=8)
with xnano.new(width=24, height=8) as canvas:
canvas.screen.write(chart)
assert 'time' in canvas.screen.render()
ChartDataset
module-attribute
¶
ChartDataset = _CoreChartDataset
A series of (x, y) points that a Chart draws, with a
name, a marker and a style.
Give a chart one dataset for every series it plots.
import xnano
from xnano.widgets import Chart, ChartDataset
series = ChartDataset(name='load', data=[(0, 0), (1, 1)], graph_type='line')
series.data = [(0, 1), (1, 0)]
with xnano.new(width=20, height=8) as canvas:
canvas.screen.write(Chart([series], width=20, height=8))
assert series.data[0] == (0.0, 1.0)
assert canvas.screen.render().strip()
Figure
module-attribute
¶
Figure = _CoreCanvas
A coordinate plane that shapes, points and labels are drawn on; it is not a
Canvas.
Reach for it to draw lines, circles, maps and labels at coordinates of your own; its
shapes are the FigureXxx classes of this module. They are the classes that
xnano_core calls Canvas, CanvasCircle, CanvasLine and so on, so their
reprs and type names read that way.
Maps to xnano_core.Canvas.
FigureCircle
module-attribute
¶
FigureCircle = _CoreCanvasCircle
A circle outline on a Figure.
Add it to a figure with its center and radius in the figure's coordinates.
Maps to xnano_core.CanvasCircle.
FigureLabel
module-attribute
¶
FigureLabel = _CoreCanvasLabel
Text printed at a position of a Figure, always on top of its
shapes.
Use it to name a point or a shape.
Maps to xnano_core.CanvasLabel.
FigureLine
module-attribute
¶
FigureLine = _CoreCanvasLine
A straight line between two points of a Figure, optionally
filled down to a baseline.
Use it for segments, axes and outlines.
Maps to xnano_core.CanvasLine.
FigureMap
module-attribute
¶
FigureMap = _CoreCanvasMap
The map of the world on a Figure, in longitude and latitude.
Give the figure the bounds (-180, 180) and (-90, 90) so the map fills it.
Maps to xnano_core.CanvasMap.
FigurePoints
module-attribute
¶
FigurePoints = _CoreCanvasPoints
A set of single points on a Figure.
Use it for scatter plots and markers.
Maps to xnano_core.CanvasPoints.
import xnano
from xnano.widgets import Figure, FigurePoints
points = FigurePoints([(1, 1), (5, 5)], 'yellow')
figure = Figure((0, 10), (0, 10), marker='block', shapes=[points], width=10, height=5)
with xnano.new(width=10, height=5) as canvas:
canvas.screen.write(figure)
assert len(points) == 2
assert '█' in canvas.screen.render()
FigureRectangle
module-attribute
¶
FigureRectangle = _CoreCanvasRectangle
An axis-aligned rectangle outline on a Figure.
Add it to a figure with its lower-left corner and its size in the figure's coordinates.
Maps to xnano_core.CanvasRectangle.
import xnano
from xnano.widgets import Figure, FigureRectangle
frame = FigureRectangle(0, 0, 10, 10, 'red')
figure = Figure((0, 10), (0, 10), marker='block', shapes=[frame], width=10, height=5)
with xnano.new(width=10, height=5) as canvas:
canvas.screen.write(figure)
assert '█' in canvas.screen.render()
Gauge
module-attribute
¶
Gauge = _CoreGauge
A bar filled to a ratio, with an optional label.
Reach for it to show progress; the ratio can be an expression that follows a value.
import xnano
from xnano.widgets import Gauge
gauge = Gauge(0.5, label='half', width=12, height=1)
with xnano.new(width=12, height=1) as canvas:
canvas.screen.write(gauge)
gauge.ratio = 0.75
assert 'half' in canvas.screen.render()
ItemList
module-attribute
¶
ItemList = _CoreItemList
A list of items, one of which can be selected.
Reach for it for menus and pickers; move the selection with select_next and its
relatives.
import xnano
from xnano.widgets import ItemList
items = ItemList(['one', 'two', 'three'], selected=1, highlight_symbol='> ', width=12, height=3)
with xnano.new(width=12, height=3) as canvas:
canvas.screen.write(items)
assert '> two' in canvas.screen.render()
LineGauge
module-attribute
¶
LineGauge = _CoreLineGauge
A progress indicator drawn with a thin line.
Use it instead of a Gauge when the indicator should take one
row and little weight.
ListItem
module-attribute
¶
ListItem = _CoreListItem
One entry of an ItemList, with its own style.
Use it when an item needs a look of its own; a plain string is enough otherwise.
import xnano
from xnano.styles import Style
from xnano.widgets import ItemList, ListItem
items = ItemList([ListItem('urgent', style=Style('red')), 'later'], width=10, height=2)
with xnano.new(width=10, height=2) as canvas:
canvas.screen.write(items)
assert canvas.screen.render().splitlines() == ['urgent', 'later']
Paragraph
module-attribute
¶
Paragraph = _CoreParagraph
Styled text that wraps, aligns and scrolls.
Reach for it when text has to fit a width or sit inside a
Block.
import xnano
from xnano.widgets import Paragraph
paragraph = Paragraph('hello brave new world', should_wrap=True, width=10, height=3)
with xnano.new(width=10, height=3) as canvas:
canvas.screen.write(paragraph)
assert canvas.screen.render().splitlines() == ['hello', 'brave new', 'world']
Scrollbar
module-attribute
¶
Scrollbar = _CoreScrollbar
A bar that shows where the visible part sits within longer content.
Draw one beside a list or a paragraph and move it with next and previous.
Sparkline
module-attribute
¶
Sparkline = _CoreSparkline
A compact history of numbers drawn as small bars.
Reach for it to show a trend in a row or two; append adds a sample.
import xnano
from xnano.widgets import Sparkline
sparkline = Sparkline([1, 3, 2, 5], width=5, height=1)
with xnano.new(width=5, height=1) as canvas:
canvas.screen.write(sparkline)
sparkline.append(4)
assert len(sparkline.values) == 5
assert canvas.screen.render().strip()
SparklineBar
module-attribute
¶
SparklineBar = _CoreSparklineBar
One sample of a Sparkline with a style of its own.
Use it to color the samples that matter.
Table
module-attribute
¶
Table = _CoreTable
Rows and columns of text with an optional header and a selection.
Reach for it for tabular data; a column width is a number of cells or a
SizeRule.
import xnano
from xnano.widgets import Table
table = Table(
[['ada', '36'], ['alan', '41']],
header=['name', 'age'],
widths=[6, 4],
width=12,
height=3,
)
with xnano.new(width=12, height=3) as canvas:
canvas.screen.write(table)
assert canvas.screen.render().splitlines()[0].split() == ['name', 'age']
TableCell
module-attribute
¶
TableCell = _CoreTableCell
One cell of a TableRow, with its own style and a column
span.
Use it when a cell needs a look of its own or spans several columns; a plain string is enough otherwise.
import xnano
from xnano.styles import Style
from xnano.widgets import Table, TableCell, TableRow
row = TableRow([TableCell('total', style=Style(bold=True)), '77'])
with xnano.new(width=12, height=1) as canvas:
canvas.screen.write(Table([row], widths=[6, 4], width=12, height=1))
assert canvas.screen.render().split() == ['total', '77']
TableRow
module-attribute
¶
TableRow = _CoreTableRow
One row of a Table, with a height and margins.
Use it when a row is taller than one line or needs a style of its own.
import xnano
from xnano.widgets import Table, TableRow
row = TableRow(['ada', '36'], height=2)
with xnano.new(width=12, height=2) as canvas:
canvas.screen.write(Table([row], widths=[6, 4], width=12, height=2))
assert canvas.screen.render().split() == ['ada', '36']
Tabs
module-attribute
¶
Tabs = _CoreTabs
A row of titles, one of which is selected.
Reach for it to switch between views; move the selection with select_next and
select_previous.
Widget
module-attribute
¶
Widget = _CoreWidget
The base class of every widget: a buffer painted from settings that can be expressions.
Use it in isinstance checks and annotations for code that accepts any widget.
__getattr__
¶
Import an object from xnano_core the first time it is read, then keep it.
Source code in xnano/widgets.py
def __getattr__(name: str) -> object:
"""Import an object from ``xnano_core`` the first time it is read, then keep it."""
core_name = _CORE_NAMES.get(name)
if core_name is None:
raise AttributeError(
f'xnano.widgets has no name {name!r}. Its names are {", ".join(__all__)}.'
)
import xnano_core
value = getattr(xnano_core, core_name)
globals()[name] = value
return value