Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements to pl-matrix-output element #1453

Merged
merged 30 commits into from Mar 31, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
89a673f
Closes #1365 and will add local digit control
eliving2 Mar 7, 2019
1761d22
Added local digit control
eliving2 Mar 7, 2019
a254d7e
Added to changelog and readme, fixed python lint
eliving2 Mar 7, 2019
3af704b
Requested changes to element and doc
lizliv Mar 12, 2019
488dca9
New example question outline started
lizliv Mar 12, 2019
94ca689
Merge branch 'master' into matrix-ouput-update
nwalters512 Mar 12, 2019
b0caa5a
Added matrix output boxes to example question
eliving2 Mar 12, 2019
f523389
Completed example question/Added tab-display attributes
lizliv Mar 12, 2019
6b04794
Style fix
lizliv Mar 12, 2019
a90e3a6
Added language='mathematica' to pl.string_from_2darray
eliving2 Mar 12, 2019
57b56ec
Update ChangeLod.md
eliving2 Mar 12, 2019
06829a5
Style fix
eliving2 Mar 12, 2019
ed47197
Requested changes implemented
eliving2 Mar 13, 2019
3e92f09
Fixed up example question
eliving2 Mar 13, 2019
9b286b8
More sensical tab control
eliving2 Mar 13, 2019
93a7818
Changed element to pl-variable-output
lizliv Mar 14, 2019
befa2fa
Small style/typo fixes
lizliv Mar 14, 2019
7b8ddca
Added 1D vector support for the element
eliving2 Mar 14, 2019
37c5633
Python lint...
eliving2 Mar 14, 2019
9e2bf10
Fixed selection of default tab
eliving2 Mar 14, 2019
9e9884e
Changed active tab control to also be an array
eliving2 Mar 14, 2019
4263152
Reverted to previous indentation
eliving2 Mar 14, 2019
c6ac880
Small fixes to pl-variable-output
eliving2 Mar 17, 2019
5e20a18
Changed `string_from_2darray` to `string_from_numpy`
eliving2 Mar 18, 2019
18b0f99
Fixed comment spacing and made variables in example unique
eliving2 Mar 18, 2019
fb9dd95
Merge branch 'master' into matrix-ouput-update
eliving2 Mar 30, 2019
99d5db2
Merged master into branch, fixed changelog spacing
eliving2 Mar 30, 2019
b4f694a
Added deprecated `string_from_2darray()`
eliving2 Mar 30, 2019
caa9f15
Added to/reworded changes in changelog
eliving2 Mar 30, 2019
846d035
Python lint
eliving2 Mar 30, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions ChangeLog.md
Expand Up @@ -151,6 +151,10 @@

* Remove old temporary upgrade flag `tmp_upgraded_iq_status` (Matt West).

* Add Mathematica tab to `pl-matrix-output` (Liz Livingston)

* Add comment and local digit control for `pl-matrix-output`, optional `params-comment` and `params-digits` (Liz Livingston)

eliving2 marked this conversation as resolved.
Show resolved Hide resolved
* __3.1.0__ - 2018-10-08

* Add string input element (Mariana Silva).
Expand Down
13 changes: 10 additions & 3 deletions doc/elements.md
Expand Up @@ -213,19 +213,26 @@ Attributes for `<variable>` (one of these for each variable to display):
Attribute | Type | Default | Description
--- | --- | --- | ---
`params-name` | string | — | Name of variable in `data['params']` to display.
`params-comment` | string | — | Name of variable in `data['params']` to display.
eliving2 marked this conversation as resolved.
Show resolved Hide resolved
`params-digits` | integer | — | Number of digits to display after the decimal for the variable.
eliving2 marked this conversation as resolved.
Show resolved Hide resolved

This element displays a list of variables inside `<pre>` tags that are formatted for import into either MATLAB or python (the user can switch between the two). Each variable must be either a scalar or a 2D numpy array (expressed as a list). Each variable will be prefixed by the text that appears between the `<variable>` and `</variable>` tags, followed by ` = `.
This element displays a list of variables inside `<pre>` tags that are formatted for import into either MATLAB, Mathematica, or python (the user can switch between the two). Each variable must be either a scalar or a 2D numpy array (expressed as a list). Each variable will be prefixed by the text that appears between the `<variable>` and `</variable>` tags, followed by ` = `.
eliving2 marked this conversation as resolved.
Show resolved Hide resolved

Here is an example of MATLAB format:
```
A = [1.23; 4.56];
A = [1.23; 4.56]; % matrix
```

Here is an example of the Mathematica format:
```
A = [1.23; 4.56]; (* matrix *)
```

Here is an example of python format:
```
import numpy as np

A = np.array([[1.23], [4.56]])
A = np.array([[1.23], [4.56]]) # matrix
```

If a variable `v` is a complex object, you should use `import prairielearn as pl` and `data['params'][params-name] = pl.to_json(v)`.
Expand Down
7 changes: 7 additions & 0 deletions elements/pl-matrix-output/pl-matrix-output.mustache
Expand Up @@ -8,6 +8,7 @@
<div class="card-header">
<ul class="nav nav-tabs card-header-tabs" role="tablist">
<li class="nav-item" role="presentation"><a class="nav-link{{#default_is_matlab}} active{{/default_is_matlab}}" href="#matlab-{{uuid}}" aria-controls="matlab-{{uuid}}" role="tab" data-toggle="pill">matlab</a></li>
<li class="nav-item" role="presentation"><a class="nav-link{{#default_is_mathematica}} active{{/default_is_mathematica}}" href="#mathematica-{{uuid}}" aria-controls="mathematica-{{uuid}}" role="tab" data-toggle="pill">mathematica</a></li>
eliving2 marked this conversation as resolved.
Show resolved Hide resolved
<li class="nav-item" role="presentation"><a class="nav-link{{#default_is_python}} active{{/default_is_python}}" href="#python-{{uuid}}" aria-controls="python-{{uuid}}" role="tab" data-toggle="pill">python</a></li>
</ul>
</div>
Expand All @@ -19,6 +20,12 @@
copy this text
</button>
</div>
<div role="tabpanel" class="tab-pane {{#default_is_mathematica}}active{{/default_is_mathematica}}" id="mathematica-{{uuid}}">
<pre class="bg-dark text-white rounded p-2" id="mathematica-data-{{uuid}}">{{mathematica_data}}</pre>
<button type="button" class="btn btn-secondary btn-sm copy-button" data-clipboard-target="#mathematica-data-{{uuid}}">
copy this text
</button>
</div>
<div role="tabpanel" class="tab-pane {{#default_is_python}}active{{/default_is_python}}" id="python-{{uuid}}">
<pre class="bg-dark text-white rounded p-2" id="python-data-{{uuid}}">{{python_data}}</pre>
<button type="button" class="btn btn-secondary btn-sm copy-button" data-clipboard-target="#python-data-{{uuid}}">
Expand Down
34 changes: 31 additions & 3 deletions elements/pl-matrix-output/pl-matrix-output.py
Expand Up @@ -14,11 +14,12 @@ def render(element_html, data):
digits = pl.get_integer_attrib(element, 'digits', 2)

matlab_data = ''
mathematica_data = ''
python_data = 'import numpy as np\n\n'
for child in element:
if child.tag == 'variable':
# Raise exception of variable does not have a name
pl.check_attribs(child, required_attribs=['params-name'], optional_attribs=[])
pl.check_attribs(child, required_attribs=['params-name'], optional_attribs=['params-comment', 'params-digits'])
eliving2 marked this conversation as resolved.
Show resolved Hide resolved

# Get name of variable
var_name = pl.get_string_attrib(child, 'params-name')
Expand All @@ -32,6 +33,19 @@ def render(element_html, data):
# back to a standard type (otherwise, do nothing)
var_data = pl.from_json(var_data)

# Get comment, if it exists
if pl.has_attrib(child, 'params-comment') is False:
commentExists = 0
else:
var_comment = pl.get_string_attrib(child, 'params-comment')
commentExists = 1

# Get digit for child, if it exists
if pl.has_attrib(child, 'params-digits') is False:
var_digits = digits
else:
var_digits = pl.get_string_attrib(child, 'params-digits')

if np.isscalar(var_data):
prefix = ''
suffix = ''
Expand All @@ -45,13 +59,27 @@ def render(element_html, data):
prefix = 'np.array('
suffix = ')'

# Mathematica reserved letters: C D E I K N O
mathematica_reserved = ['C', 'D', 'E', 'I', 'K', 'N', 'O']
if pl.inner_html(child) in mathematica_reserved:
mathematica_suffix = 'm'
else:
mathematica_suffix = ''

# Create string for matlab and python format
matlab_data += pl.inner_html(child) + ' = ' + pl.string_from_2darray(var_data, language='matlab', digits=digits) + ';\n'
python_data += pl.inner_html(child) + ' = ' + prefix + pl.string_from_2darray(var_data, language='python', digits=digits) + suffix + '\n'
if commentExists == 0:
eliving2 marked this conversation as resolved.
Show resolved Hide resolved
matlab_data += pl.inner_html(child) + ' = ' + pl.string_from_2darray(var_data, language='matlab', digits=var_digits) + ';\n'
mathematica_data += pl.inner_html(child) + ' = ' + pl.string_from_2darray(var_data, language='matlab', digits=var_digits) + ';\n'
python_data += pl.inner_html(child) + ' = ' + prefix + pl.string_from_2darray(var_data, language='python', digits=var_digits) + suffix + '\n'
eliving2 marked this conversation as resolved.
Show resolved Hide resolved
else:
matlab_data += pl.inner_html(child) + ' = ' + pl.string_from_2darray(var_data, language='matlab', digits=var_digits) + '; % ' + var_comment + '\n'
mathematica_data += pl.inner_html(child) + mathematica_suffix + ' = ' + pl.string_from_2darray(var_data, language='matlab', digits=var_digits) + '; (* ' + var_comment + ' *)\n'
python_data += pl.inner_html(child) + ' = ' + prefix + pl.string_from_2darray(var_data, language='python', digits=var_digits) + suffix + ' # ' + var_comment + '\n'

html_params = {
'default_is_matlab': True,
eliving2 marked this conversation as resolved.
Show resolved Hide resolved
'matlab_data': matlab_data,
'mathematica_data': mathematica_data,
'python_data': python_data,
'uuid': pl.get_uuid()
}
Expand Down