ryantam626 / jupyterlab_black

A JupyterLab extension to apply Black formatter to code within codecell.
MIT License
37 stars 2 forks source link

Not working on Windows #8

Open melund opened 6 years ago

melund commented 6 years ago

The formatter doesn't work on Windows.

It would probably work if it used tempfile from the standard lib instead of relying on "/tmp/" always being present.

I get the following when running the formatter:

    HTTPServerRequest(protocol='http', host='localhost:8888', method='POST', uri='/jupyterlab_black/format?1536823981888', version='HTTP/1.1', remote_ip='::1')
    Traceback (most recent call last):
      File "C:\Users\mel\anaconda3\envs\ipython\lib\site-packages\tornado\web.py", line 1590, in _execute
        result = method(*self.path_args, **self.path_kwargs)
      File "C:\Users\mel\anaconda3\envs\ipython\lib\site-packages\jupyterlab_black\handlers.py", line 36, in post
        with open("/tmp/jupyrerlab_black_worker.py", "w") as file_:
    FileNotFoundError: [Errno 2] No such file or directory: '/tmp/jupyrerlab_black_worker.py'
[W 09:33:01.904 LabApp] Unhandled error
[E 09:33:01.905 LabApp] {
      "Host": "localhost:8888",
      "Connection": "keep-alive",
      "Content-Length": "270",
      "Authorization": "token 4a71058a2add832e29638427cff8a9dcad6c020bf62eb394",
      "Origin": "http://localhost:8888",
      "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36",
      "Content-Type": "application/json",
      "Accept": "*/*",
      "Referer": "http://localhost:8888/lab",
      "Accept-Encoding": "gzip, deflate, br",
      "Accept-Language": "da-DK,da;q=0.9,en-DK;q=0.8,en;q=0.7,en-US;q=0.6",
      "Cookie": "_xsrf=2|79b9938f|7c699e97e3b5bdee56707a51dd633955|1536130147; username-localhost-8889=\"2|1:0|10:1536301346|23:username-localhost-8889|44:YmNkMDQ1ZDNlZWFmNGE2MTk5MGI4MmY5NmI5NjUyNzQ=|6ac5ca7d28af0515e042887b5ed5b320620ee49e3a22b0c7048f02dd88213105\"; username-localhost-8888=\"2|1:0|10:1536823980|23:username-localhost-8888|44:MmIwYWQzOGQ2Mzk5NGJkMjg1NTM1ZDU4NDhkMmNjMTk=|e7a7ab793d0b1809c108c3304bfa5ce6f319bd1ec0b9943ca6202858486e04a4\""
    }
[E 09:33:01.911 LabApp] 500 POST /jupyterlab_black/format?1536823981888 (::1) 13.00ms referer=http://localhost:8888/lab
ryantam626 commented 6 years ago

Good point, will patch this tonight when I get home :+1:

melund commented 6 years ago

@ryantam626 That would be great. I am sure it would work then.

ryantam626 commented 6 years ago

Hello! Apologies for the delay, got caught up with other stuff :frowning:

09b34723abfb1c7120d7f33003c7b384333555cb should fix it now, you can probably do pip install https://github.com/ryantam626/jupyterlab_black/archive/master.zip again to grab it :+1:

Let me know if it still doesn't work for you (I can only test with my Linux machines at the moment)

melund commented 6 years ago

No problem. I still get an error though. Looks like there are two issues at play, but it is late here and I am too tired to dive into it right now. But here is the traceback for reference:

[I 23:09:50.486 LabApp] Build is up to date
[I 23:09:51.430 LabApp] Kernel started: 4ad21ed2-dcec-4fec-9f00-09804210dbb5
[I 23:09:53.165 LabApp] Adapting to protocol v5.1 for kernel 4ad21ed2-dcec-4fec-9f00-09804210dbb5
[I 23:09:53.166 LabApp] Adapting to protocol v5.1 for kernel 4ad21ed2-dcec-4fec-9f00-09804210dbb5
C:/Users/mel/anaconda3/python.exe: can't open file 'C:\Users\mel\AppData\Local\Temp\tmpbhiyrr0u': [Errno 13] Permission denied
[E 23:10:01.548 LabApp] {
      "Host": "localhost:8888",
      "Connection": "keep-alive",
      "Content-Length": "245",
      "Authorization": "token a4d07ee6e2eafaada5505e23d590a54784f92cf2ebdfe33b",
      "Origin": "http://localhost:8888",
      "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36",
      "Content-Type": "application/json",
      "Accept": "*/*",
      "Referer": "http://localhost:8888/lab",
      "Accept-Encoding": "gzip, deflate, br",
      "Accept-Language": "da-DK,da;q=0.9,en-DK;q=0.8,en;q=0.7,en-US;q=0.6",
      "Cookie": "_xsrf=2|79b9938f|7c699e97e3b5bdee56707a51dd633955|1536130147; username-localhost-8889=\"2|1:0|10:1536301346|23:username-localhost-8889|44:YmNkMDQ1ZDNlZWFmNGE2MTk5MGI4MmY5NmI5NjUyNzQ=|6ac5ca7d28af0515e042887b5ed5b320620ee49e3a22b0c7048f02dd88213105\"; username-localhost-8888=\"2|1:0|10:1537218600|23:username-localhost-8888|44:YTY3MWRiZDYyOTk2NDE2OThjNTdkNWRlYWJiZGI5NDY=|b7a9c59c57e2f18997e802daeeeac1c567a102aa6c2025a607c51de5e76e1147\""
    }
[E 23:10:01.555 LabApp] 500 POST /jupyterlab_black/format?1537218601386 (::1) 158.98ms referer=http://localhost:8888/lab
[E 23:10:01.558 LabApp] Uncaught exception POST /jupyterlab_black/format?1537218601386 (::1)
    HTTPServerRequest(protocol='http', host='localhost:8888', method='POST', uri='/jupyterlab_black/format?1537218601386', version='HTTP/1.1', remote_ip='::1')
    Traceback (most recent call last):
      File "C:\Users\mel\anaconda3\envs\ipython\lib\site-packages\tornado\web.py", line 1590, in _execute
        result = method(*self.path_args, **self.path_kwargs)
      File "C:\Users\mel\anaconda3\envs\ipython\lib\site-packages\jupyterlab_black\handlers.py", line 65, in post
        self.finish(json.dumps(formatted))
      File "C:\Users\mel\anaconda3\envs\ipython\lib\site-packages\notebook\base\handlers.py", line 603, in finish
        return super(APIHandler, self).finish(*args, **kwargs)
      File "C:\Users\mel\anaconda3\envs\ipython\lib\site-packages\tornado\web.py", line 1022, in finish
        raise RuntimeError("finish() called twice")
    RuntimeError: finish() called twice
ryantam626 commented 6 years ago

You can safe ignore RuntimeError: finish() called twice - this is due to me not understanding how tornado works ☹️

The Permission denied thing is the issue here - this doesn't look trivial to solve, with the way how access permissions is decided in the files tempfile generated in Windows 🤔 need a little think about this hmm..

melund commented 6 years ago

@ryantam626 There is a Gotcha in the Python docs for NamedTemporaryFile().

Whether the name can be used to open the file a second time, while the named temporary file is still open, varies across platforms (it can be so used on Unix; it cannot on Windows NT or later).

Maybe the best option is to use tempfile.TemporaryDirectory or temp.mkdtemp to create a directory and then create theworker and formatter files in there. That should workaround the problem.

xujinzh commented 5 years ago

not work