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

Table View renders CSV file incorrectly #4795

Closed
groutr opened this issue Jun 28, 2018 · 7 comments
Closed

Table View renders CSV file incorrectly #4795

groutr opened this issue Jun 28, 2018 · 7 comments

Comments

@groutr
Copy link
Contributor

@groutr groutr commented Jun 28, 2018

The table view in jupyterlab 0.32.1 seems to render the csv generated here incorrectly.

import csv
with open('test.csv', 'w') as writefile:
    writer = csv.DictWriter(writefile, ['a', 'b', 'c'])
    writer.writeheader()

Opening test.csv in table view produces
bad_csv.png

@jasongrout
Copy link
Contributor

@jasongrout jasongrout commented Jun 28, 2018

What is the text that outputs?

@jasongrout
Copy link
Contributor

@jasongrout jasongrout commented Jun 28, 2018

I get an error

Python 3.6.3 | packaged by conda-forge | (default, Dec  9 2017, 16:20:51) 
Type 'copyright', 'credits' or 'license' for more information
IPython 6.2.1 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import csv
   ...: with open('test.csv', 'w') as writefile:
   ...:     writer = csv.DictWriter(writefile, ['a', 'b', 'c'])
   ...:     writer.write_header()
   ...:     
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-1-8b6b60f8a73a> in <module>()
      2 with open('test.csv', 'w') as writefile:
      3     writer = csv.DictWriter(writefile, ['a', 'b', 'c'])
----> 4     writer.write_header()
      5 

AttributeError: 'DictWriter' object has no attribute 'write_header'

@jasongrout
Copy link
Contributor

@jasongrout jasongrout commented Jun 28, 2018

Ah, it's writeheader, not write_header. Here is the contents of the file: b'a,b,c\r\n'. And yes, I see the same issue.

@jasongrout jasongrout added this to the Beta 3 milestone Jun 28, 2018
@groutr
Copy link
Contributor Author

@groutr groutr commented Jun 28, 2018

My bad, write_header() should be writeheader(). I was typing it from memory. I fixed it above.

@groutr
Copy link
Contributor Author

@groutr groutr commented Jun 28, 2018

It doesn't like seem to like one line CSV files at all.
b'a,b,c', b'a,b,c\n' give the same result. Using a different delimiter (in this case \t) also yielded the same result

It does produce the 3 columns, and if one line is added (even an empty line), the file then renders correctly.

@jasongrout
Copy link
Contributor

@jasongrout jasongrout commented Jun 28, 2018

Thanks. I think that's hopefully enough to track down the issue (feel free to take a crack at it, the code is in https://github.com/jupyterlab/jupyterlab/tree/master/packages/csvviewer).

The model is constructed with basically default args:

this._grid.model = new DSVModel({ data, delimiter });

That means that 'header' is true:
That means we should be caching the header row:
// Cache the header row.
if (header === true && this._columnCount > 0) {
let h = [];
for (let c = 0; c < this._columnCount; c++) {
h.push(this._getField(0, c));
}
this._header = h;

So question 1: is that header it is computing correct? question 2: if not, is it the parsing that is messing up?

We should add a single-line csv as a test too: https://github.com/jupyterlab/jupyterlab/blob/master/tests/test-csvviewer/src/model.spec.ts (perhaps also in the parsing tests for both the no-quotes and quoted parser).

@jasongrout
Copy link
Contributor

@jasongrout jasongrout commented Jun 28, 2018

This issue should be self-contained enough to be an advanced good first issue.

jasongrout added a commit to jasongrout/jupyterlab that referenced this issue Jun 28, 2018
@lock lock bot locked as resolved and limited conversation to collaborators Aug 8, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

2 participants