Ejemplo n.º 1
0
def test_empty_message():
    # GH 52
    messages = [{
        'parent_header': {
            'username': '******',
            'session': 'a',
            'msg_type': 'execute_request',
            'msg_id': '3',
            'date': datetime.datetime(2016, 9, 27, 7, 20, 13, 790481),
            'version': '5.0'
        },
        'metadata': {},
        'buffers': [],
        'msg_type': 'display_data',
        'header': {
            'username': '******',
            'session': 'a',
            'msg_type': 'display_data',
            'version': '5.0',
            'date': '2016-09-27T07:20:17.461893',
            'msg_id': '6'
        },
        'content': {
            'metadata': {},
            'data': {}
        },
        'msg_id': '6'
    }]
    s = R.Stitch('foo', 'html')
    result = s.wrap_output('bar', messages, {})
    assert result == []
Ejemplo n.º 2
0
    def test_error(self):
        s = R.Stitch('stdout', 'html')
        assert s.error == 'continue'
        s.error = 'raise'
        assert s.error == 'raise'

        with pytest.raises(TraitError):
            s.error = 'foo'
Ejemplo n.º 3
0
    def test_getattr(self):
        s = R.Stitch('stdout', 'html')
        assert getattr(s, 'fig.width') is None
        assert s.fig.width is None
        with pytest.raises(AttributeError):
            assert getattr(s, 'foo.bar')

        with pytest.raises(AttributeError):
            assert getattr(s, 'foo')
Ejemplo n.º 4
0
 def test_ipython_display(self, clean_python_kernel, to):
     s = R.Stitch('stdout', to)
     code = dedent('''\
     from IPython import display
     import math
     display.Markdown("$\\alpha^{pi:1.3f}$".format(pi=math.pi))
     ''')
     messages = R.run_code(code, clean_python_kernel)
     wrapped = s.wrap_output('', messages, None)[0]
     assert wrapped['t'] == 'Para'
     assert wrapped['c'][0]['c'][0]['t'] == 'InlineMath'
Ejemplo n.º 5
0
 def test_image_chunkname(self):
     code = dedent('''\
     ```{python, chunk}
     %matplotlib inline
     import matplotlib.pyplot as plt
     plt.plot(range(4), range(4));
     ```
     ''')
     result = R.Stitch('foo', 'latex',
                       standalone=False).stitch_ast(pre_stitch_ast(code))
     blocks = result['blocks']
     assert 'chunk' in blocks[1]['c'][0]['c'][0][0]
Ejemplo n.º 6
0
    def test_error_raises(self):
        s = R.Stitch('stdout', 'html', error='raise')
        code = dedent('''\
        ```{python}
        1 / 0
        ```
        ''')
        with pytest.raises(R.KnittyError):
            s.stitch_ast(pre_stitch_ast(code))

        s.error = 'continue'
        s.stitch_ast(pre_stitch_ast(code))
Ejemplo n.º 7
0
 def test_warning(self, clean_python_kernel, warning, length):
     code = dedent('''\
     ```{python}
     import warnings
     warnings.warn("Hi")
     2
     ```
     ''')
     r = R.Stitch('foo', 'html', warning=warning)
     r._kernel_pairs['python'] = clean_python_kernel
     result = r.stitch_ast(pre_stitch_ast(code))
     assert len(result['blocks']) == length
Ejemplo n.º 8
0
 def test_image(self, to, value, global_python_kernel):
     code = dedent('''\
     ```{python}
     %matplotlib inline
     import matplotlib.pyplot as plt
     plt.plot(range(4), range(4))
     plt.title('Foo — Bar');  # That's an em dash
     ```
     ''')
     result = R.Stitch('foo', to).stitch_ast(pre_stitch_ast(code))
     blocks = result['blocks']
     assert blocks[1]['c'][0]['t'] == 'Image'
Ejemplo n.º 9
0
 def test_rich_output(self, to, clean_python_kernel):
     code = dedent('''\
     ```{python}
     import pandas as pd
     pd.options.display.latex.repr = True
     pd.DataFrame({'a': [1, 2]})
     ```
     ''')
     stitch = R.Stitch('foo', to)
     stitch._kernel_pairs['python'] = clean_python_kernel
     blocks = stitch.stitch_ast(pre_stitch_ast(code))['blocks']
     result = blocks[1]['c'][1]
     assert '\\begin{tabular}' in result
Ejemplo n.º 10
0
 def test_image_attrs(self):
     code = dedent('''\
     ```{python, chunk, fig.width=10, fig.height=10px}
     %matplotlib inline
     import matplotlib.pyplot as plt
     plt.plot(range(4), range(4));
     ```
     ''')
     result = R.Stitch('foo', 'html',
                       standalone=False).stitch_ast(pre_stitch_ast(code))
     blocks = result['blocks']
     attrs = blocks[1]['c'][0]['c'][0][2]
     assert ('width', '10') in attrs
     assert ('height', '10px') in attrs
Ejemplo n.º 11
0
 def test_image_no_self_contained(self, clean_python_kernel, clean_name):
     code = dedent('''\
     ```{python}
     %matplotlib inline
     import matplotlib.pyplot as plt
     plt.plot(range(4))
     ```
     ''')
     s = R.Stitch(clean_name, 'html', self_contained=False)
     s._kernel_pairs['python'] = clean_python_kernel
     result = s.stitch_ast(pre_stitch_ast(code))
     blocks = result['blocks']
     expected = ('{}_files/unnamed_chunk_0.png'.format(clean_name),
                 r'{}_files\unnamed_chunk_0.png'.format(clean_name))
     result = blocks[-1]['c'][0]['c'][2][0]
     assert result in expected
Ejemplo n.º 12
0
    def test_image_no_self_contained_formats(self, clean_python_kernel,
                                             clean_name, fmt):
        code = dedent('''\
        ```{{python}}
        %matplotlib inline
        from IPython.display import set_matplotlib_formats
        import numpy as np
        import matplotlib.pyplot as plt
        set_matplotlib_formats('{fmt}')

        x = np.linspace(-np.pi / 2, np.pi / 2)
        plt.plot(x, np.sin(x))
        plt.plot(x, np.cos(x))
        ```
        ''').format(fmt=fmt)
        s = R.Stitch(clean_name, 'html', self_contained=False)
        s._kernel_pairs['python'] = clean_python_kernel
        s.stitch_ast(pre_stitch_ast(code))
        expected = p.join(clean_name + '_files', 'unnamed_chunk_0.' + fmt)
        assert p.exists(expected)
Ejemplo n.º 13
0
 def test_has_trait(self):
     s = R.Stitch('stdout', 'html')
     assert s.has_trait('fig.width')
     assert not s.has_trait('fake.width')
     assert not s.has_trait('fig.fake')
Ejemplo n.º 14
0
 def test_from_source(self, document, clean_stdout):
     R.Stitch('stdout', 'html',
              self_contained=False).stitch_ast(pre_stitch_ast(document))
Ejemplo n.º 15
0
 def test_from_file(self, document_path, clean_stdout):
     with open(document_path, 'r', encoding='utf-8') as f:
         R.Stitch('stdout', 'html',
                  self_contained=False).stitch_ast(pre_stitch_ast(f.read()))
Ejemplo n.º 16
0
 def test_wrap_output(self, output, message, expected):
     result = R.Stitch('stdout', 'html').wrap_output(output, message, {})
     assert result == expected