IPython.notebook.kernel.execute() won't let me assign js UTF-8 strings

39 views
Skip to first unread message

Harris Joseph

unread,
Dec 20, 2017, 1:23:28 AM12/20/17
to Project Jupyter

To avoid collisions on the server's filesystem, I am trying to upload csv files as strings directly into the python kernel using javascript and kernel.execute() after converting it to UTF-8 with FileReader.readAsText().result. I have tested the javascript function and it consistently prints the entire file to the console if I put a console.log() inside the callback function where I call kernel.execute() and send it the same variable outputString that gets built intvar command = "dataString ='"+outputString+"'"; before being passed to kernel.execute. I was doing this all inside a magic %%HTML cell with <script> tags , but moved everything except the input div out and put the script in its own %%javascript cell. At first I thought I just needed to set a timeout to make sure the file got completely converted, (I know it has a built in ready check i will get to that soon) but even 20s made no difference plus it still got printed in the console every time anyway. I'm not sure, but I thought Jupyter and Python 3 used UTF-8 by default, and (supposedly) that is the default, no parameter encoding that FileReader.readAsText() returns. This could also just be me being a javascript newb and missing something about the scope between the IPython.notebook.kernel and the callback function in my script inside the notebook. I have an old version with the same code here with the <script> tags, it's the same otherwise (i think). I'll post again in the morning.

Thanks and goodnight!


Harris

Thomas Kluyver

unread,
Dec 20, 2017, 5:35:43 AM12/20/17
to Project Jupyter
Hi Harris,

Can you show the code that you're running? I'm having trouble following the description.

Thomas

--
You received this message because you are subscribed to the Google Groups "Project Jupyter" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jupyter+unsubscribe@googlegroups.com.
To post to this group, send email to jup...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jupyter/ea080396-e87a-4801-bdca-c09391d03f86%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Harris Joseph

unread,
Dec 22, 2017, 1:58:35 AM12/22/17
to Project Jupyter
Hi Thomas and thanks for getting back!

I actually went with a workaround where I converted the csv to a 2d javascript, shifting off the top row for keys, transposing the rest and then made a method called pythonify which converted the rows to the properly formatted strings to send and built a list of lists using notebook.kernel.execute("cols.append("+pythonify(column)+")"); in a loop. I documented it here:


https://stackoverflow.com/questions/47898609/trying-to-capture-a-csv-upload-as-string-jupyter


On Wednesday, December 20, 2017 at 5:35:43 AM UTC-5, takowl wrote:
Hi Harris,

Can you show the code that you're running? I'm having trouble following the description.

Thomas
On 20 December 2017 at 06:23, Harris Joseph <harris.j...@gmail.com> wrote:

To avoid collisions on the server's filesystem, I am trying to upload csv files as strings directly into the python kernel using javascript and kernel.execute() after converting it to UTF-8 with FileReader.readAsText().result. I have tested the javascript function and it consistently prints the entire file to the console if I put a console.log() inside the callback function where I call kernel.execute() and send it the same variable outputString that gets built intvar command = "dataString ='"+outputString+"'"; before being passed to kernel.execute. I was doing this all inside a magic %%HTML cell with <script> tags , but moved everything except the input div out and put the script in its own %%javascript cell. At first I thought I just needed to set a timeout to make sure the file got completely converted, (I know it has a built in ready check i will get to that soon) but even 20s made no difference plus it still got printed in the console every time anyway. I'm not sure, but I thought Jupyter and Python 3 used UTF-8 by default, and (supposedly) that is the default, no parameter encoding that FileReader.readAsText() returns. This could also just be me being a javascript newb and missing something about the scope between the IPython.notebook.kernel and the callback function in my script inside the notebook. I have an old version with the same code here with the <script> tags, it's the same otherwise (i think). I'll post again in the morning.

Thanks and goodnight!


Harris

--
You received this message because you are subscribed to the Google Groups "Project Jupyter" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jupyter+u...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages