1

Resolved

First line of multiline outputs in IPython does not align with the remaining lines

description

In Visual Studio ipython Interactive Environment when I want to see a pandas DataFrame the first row of output is off. I have to type 'print df' in order for the columns/rows to be aligned properly. This however is not necessary in standard ipython or other IDE's such as pycharm. I love using Visual Studio for my scientific python needs but this is driving me crazy... solution to fix this???

Visual Studio 2013:
In [1]: import pandas as pd

In [2]: d = {'one': [1., 2., 3., 4.],
   ...:      'two': [5., 6., 7., 8.]}
   ...: 

In [3]: df = pd.DataFrame(d)

In [4]: df
Out[4]:    one  two
0    1    5
1    2    6
2    3    7
3    4    8
pycharm
>>> import pandas as pd
>>> d = {'one': [1., 2., 3., 4.],'two': [5., 6., 7., 8.]}
>>> df = pd.DataFrame(d)
>>> df
Out[33]: 
   one  two
0    1    5
1    2    6
2    3    7
3    4    8
iPython in Powershell
In [1]: import pandas as pd

In [2]: d = {'one': [1., 2., 3., 4.],'two': [5., 6., 7., 8.]}

In [3]: df = pd.DataFrame(d)

In [4]: df
Out[4]:
   one  two
0    1    5
1    2    6
2    3    7
3    4    8

comments

pminaev wrote Feb 7 at 9:29 PM

Thank you for reporting this problem.

We are going to fix it for 2.1 beta. However, it is actually a very simple fix that involves adding two lines of code to a .py file, so you can easily apply it to your PTVS install without having to wait for the beta.

The file that you need to edit is visualstudio_ipython_repl.py, located in %ProgramFiles(x86)%\Microsoft Visual Studio <VS version>\Common7\IDE\Extensions\Microsoft\Python Tools for Visual Studio\2.0. The code that needs to be fixed is inside function write_data, and looks like so:
        if output_str is not None:
            if execution_count is not None:
                output_str = 'Out[' + str(execution_count) + ']: ' + output_str
Change it to:
        if output_str is not None:
            if execution_count is not None:
                if '\n' in output_str:
                    output_str = '\n' + output_str
                output_str = 'Out[' + str(execution_count) + ']: ' + output_str
This should take care of the problem. Hope it helps!

ryanfdiehl wrote Feb 8 at 12:53 AM

Perfect! It worked, thank you so much!!!