Element pl-python-variable: Display of Python variables

The following examples below were created to show the capabilities of the pl-python-variable element, which allows formatting and rendering various Python variables defined in the server.py on a question page. The semantics of this element are very similar to those of the Python pprint library, and we encourage reviewing documentation for that library for more detailed examples.

These examples are drawn from the pprint library documentation and a tutorial for using this library.

Here is a randomly generated matrix created by calling np.random.random((3, 3)). Any variables will be converted using repr() if they are being shown as text.

1
2
3
array([[0.77570296, 0.07805969, 0.24925597],
       [0.96877896, 0.85404971, 0.34162242],
       [0.57096907, 0.66794662, 0.4293415 ]])

Here is an example displaying a dictionary. You can add additional text to the output by using the prefix and suffix attributes. We also set copy-code-button="True" to enable the copy code button.

1
my_dictionary = {'a': 1, 'b': 2, 'c': 3}

Here is an example displaying a string.

1
my_string = 'a string'

Here is an example displaying a dictionary with line breaks. We've set width="1" to force printing each element on a new line and set prefix-newline="true" to insert a line break between the prefix and the printed dictionary.

1
2
3
4
my_dictionary = \
{'a': 1,
 'b': 2,
 'c': 3}

Here is an example displaying a list with line breaks and a comment on the next line using the suffix and suffix-newline attributes.

1
2
3
4
5
my_list = \
['a',
 'b',
 'c']
# This is a cool list

Here is an example displaying a more complex list. We've also enabled line numbers by setting show-line-numbers="True", and compact sequences by setting compact-sequences="True".

1
2
[{'a': 1, 'aa': 1}, {'b': 2, 'bb': 2},
 {'c': 3, 'cc': 3}, {'d': 4, 'dd': 4}]

Here is an example displaying a list with indent="4".

1
2
3
4
5
6
[   ['spam', 'eggs', 'lumberjack', 'knights', 'ni'],
    'spam',
    'eggs',
    'lumberjack',
    'knights',
    'ni']

Here is an example displaying a complex dictionary with depth="3", to limit the depth of the data structures that are displayed, and indent="4", to provide some space between levels .

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
[   {   'id': 1,
        'name': 'Leanne Graham',
        'email': 'Sincere@april.biz',
        'phone': '1-770-736-8031 x56442',
        'address': {   'geo': {...},
                       'city': 'Gwenborough',
                       'suite': 'Apt. 556',
                       'street': 'Kulas Light',
                       'zipcode': '92998-3874'},
        'company': {   'bs': 'harness real-time e-markets',
                       'name': 'Romaguera-Crona',
                       'catchPhrase': 'Multi-layered client-server neural-net'},
        'website': 'hildegard.org',
        'username': 'Bret'},
    {   'id': 2,
        'name': 'Ervin Howell',
        'email': 'Shanna@melissa.tv',
        'phone': '010-692-6593 x09125',
        'address': {   'geo': {...},
                       'city': 'Wisokyburgh',
                       'suite': 'Suite 879',
                       'street': 'Victor Plains',
                       'zipcode': '90566-7771'},
        'company': {   'bs': 'synergize scalable supply-chains',
                       'name': 'Deckow-Crist',
                       'catchPhrase': 'Proactive didactic contingency'},
        'website': 'anastasia.net',
        'username': 'Antonette'},
    {   'id': 3,
        'name': 'Clementine Bauch',
        'email': 'Nathan@yesenia.net',
        'phone': '1-463-123-4447',
        'address': {   'geo': {...},
                       'city': 'McKenziehaven',
                       'suite': 'Suite 847',
                       'street': 'Douglas Extension',
                       'zipcode': '59590-4157'},
        'company': {   'bs': 'e-enable strategic applications',
                       'name': 'Romaguera-Jacobson',
                       'catchPhrase': 'Face to face bifurcated interface'},
        'website': 'ramiro.info',
        'username': 'Samantha'},
    {   'id': 4,
        'name': 'Patricia Lebsack',
        'email': 'Julianne.OConner@kory.org',
        'phone': '493-170-9623 x156',
        'address': {   'geo': {...},
                       'city': 'South Elvis',
                       'suite': 'Apt. 692',
                       'street': 'Hoeger Mall',
                       'zipcode': '53919-4257'},
        'company': {   'bs': 'transition cutting-edge web services',
                       'name': 'Robel-Corkery',
                       'catchPhrase': 'Multi-tiered zero tolerance '
                                      'productivity'},
        'website': 'kale.biz',
        'username': 'Karianne'},
    {   'id': 5,
        'name': 'Chelsey Dietrich',
        'email': 'Lucio_Hettinger@annie.ca',
        'phone': '(254)954-1289',
        'address': {   'geo': {...},
                       'city': 'Roscoeview',
                       'suite': 'Suite 351',
                       'street': 'Skiles Walks',
                       'zipcode': '33263'},
        'company': {   'bs': 'revolutionize end-to-end systems',
                       'name': 'Keebler LLC',
                       'catchPhrase': 'User-centric fault-tolerant solution'},
        'website': 'demarco.info',
        'username': 'Kamren'},
    {   'id': 6,
        'name': 'Mrs. Dennis Schulist',
        'email': 'Karley_Dach@jasper.info',
        'phone': '1-477-935-8478 x6430',
        'address': {   'geo': {...},
                       'city': 'South Christy',
                       'suite': 'Apt. 950',
                       'street': 'Norberto Crossing',
                       'zipcode': '23505-1337'},
        'company': {   'bs': 'e-enable innovative applications',
                       'name': 'Considine-Lockman',
                       'catchPhrase': 'Synchronised bottom-line interface'},
        'website': 'ola.org',
        'username': 'Leopoldo_Corkery'},
    {   'id': 7,
        'name': 'Kurtis Weissnat',
        'email': 'Telly.Hoeger@billy.biz',
        'phone': '210.067.6132',
        'address': {   'geo': {...},
                       'city': 'Howemouth',
                       'suite': 'Suite 280',
                       'street': 'Rex Trail',
                       'zipcode': '58804-1099'},
        'company': {   'bs': 'generate enterprise e-tailers',
                       'name': 'Johns Group',
                       'catchPhrase': 'Configurable multimedia task-force'},
        'website': 'elvis.io',
        'username': 'Elwyn.Skiles'},
    {   'id': 8,
        'name': 'Nicholas Runolfsdottir V',
        'email': 'Sherwood@rosamond.me',
        'phone': '586.493.6943 x140',
        'address': {   'geo': {...},
                       'city': 'Aliyaview',
                       'suite': 'Suite 729',
                       'street': 'Ellsworth Summit',
                       'zipcode': '45169'},
        'company': {   'bs': 'e-enable extensible e-tailers',
                       'name': 'Abernathy Group',
                       'catchPhrase': 'Implemented secondary concept'},
        'website': 'jacynthe.com',
        'username': 'Maxime_Nienow'},
    {   'id': 9,
        'name': 'Glenna Reichert',
        'email': 'Chaim_McDermott@dana.io',
        'phone': '(775)976-6794 x41206',
        'address': {   'geo': {...},
                       'city': 'Bartholomebury',
                       'suite': 'Suite 449',
                       'street': 'Dayna Park',
                       'zipcode': '76495-3109'},
        'company': {   'bs': 'aggregate real-time technologies',
                       'name': 'Yost and Sons',
                       'catchPhrase': 'Switchable contextually-based project'},
        'website': 'conrad.com',
        'username': 'Delphine'},
    {   'id': 10,
        'name': 'Clementina DuBuque',
        'email': 'Rey.Padberg@karina.biz',
        'phone': '024-648-3804',
        'address': {   'geo': {...},
                       'city': 'Lebsackbury',
                       'suite': 'Suite 198',
                       'street': 'Kattie Turnpike',
                       'zipcode': '31428-2261'},
        'company': {   'bs': 'target end-to-end models',
                       'name': 'Hoeger LLC',
                       'catchPhrase': 'Centralized empowering task-force'},
        'website': 'ambrose.net',
        'username': 'Moriah.Stanton'}]

Correct answer

Student view placeholder

In student views this area is used for assessment and score info.
Tools

Staff information

Question

Title:
Element pl-python-variable: Display of Python variables

Variant

Started at:
2026-08-04 21:53:28 (CDT)
Duration:
0s
Show/Hide answer
{}
History
dec frac
rad deg