365

I've been running a script on jupyter notebooks for about 26 hour; I haven't really been using my computer for anything else, but it needs to run this program that will take ~30 hours to complete. At about 21 hours in, it stopped saving and my terminal had this:

403 PUT /api/contents/[file.ipynb] (::1): '_xsrf' argument missing from POST

where [file.ipynb] is the location of my jupyter notebook. It also says:

'_xsrf' argument missing from post

in the top right part of the notebook again. The program is still running and I don't want to restart jupyter notebook and have to run the program again, as I have a deadline, is there anything else I can do?

I'm using google chrome, but I don't have the LastPass extension or any '%' characters in my code, as another post suggested.

Thanks for any help!

V P
  • 3,704
  • 2
  • 9
  • 12
  • 43
    There are discussions online on this topic if you do a search. One suggestion that I've seen made is to open the same notebook in a new browser window, which fixes the problem. Then you can close that new window and continue working in the previous one. – AlexK Mar 06 '19 at 06:26
  • Thanks @AlexK, that solves the same issue on my MBP. – RandomWalker Aug 13 '19 at 21:17
  • 2
    Just a tip, in this kind of case, store your important variables using `%store` magic. That way even if your notebook was closed and not saved, your variables still are, so you can just display them again. – tbrugere Nov 10 '21 at 03:18
  • @AlexK What about the code though? Will any changes to the code be preserved if I open in a new window? – Bill Nov 26 '21 at 22:05
  • Update on my previous comment: @AlexK's suggestion works and afterwards you can save the original notebook and any code changes made. Thanks. – Bill Nov 26 '21 at 22:09
  • To be clear, the steps that worked for me were: 1. Open the same notebook in the same browser. (note this may look like an older version) 2. Go back to the previous notebook and hit save. Then can close the recently opened notebook so no confusion. – Geronimo Aug 10 '22 at 17:19

21 Answers21

722

The easiest way I found is this:

https://github.com/nteract/hydrogen/issues/922#issuecomment-405456346

Just open another (non-running, existing) notebook on the same kernel, and the issue is magically gone; you can again save the notebooks that were previously showing the _xsrf error.

If you have already closed the Jupyter home page, you can find a link to it on the terminal from which Jupyter was started.

mirekphd
  • 4,799
  • 3
  • 38
  • 59
caxcaxcoatl
  • 8,472
  • 1
  • 13
  • 21
  • 63
    This saved my day. 4 days worth of work would have been lost, if I hadnt come across this solution. Thank you! – Hari Dattada Oct 07 '19 at 01:49
  • 28
    I did not need to open a new notebook. Instead, I reopened the [tree](http://localhost:8888/tree), and reconnected the kernel. At some point I also restarted the kernel. – user650654 Oct 09 '19 at 00:17
  • 8
    I wasn't even allowed to open a new notebook until I refresh the notebooks list. After that I opened a new notebook and the error was disappeared.. – Pavindu Apr 28 '20 at 02:36
  • 7
    Notice that the workaround requires that the notebook was created earlier, so open another existing non-running notebook, do not create a new one or open a running one, both of which would raise the same error. – mirekphd May 31 '20 at 10:44
  • 21
    This is why I love stackoverflow – Albert Chen Jun 02 '20 at 16:59
  • Refreshing the page worked for me. On top of the _xsrf` error, I got a 403 forbidden error when trying to create a new notebook. [I am accessing a local machine through port-forward.] – llinfeng Jun 14 '20 at 15:11
  • Weird. I just should note that the new copy of the workbook is not updated and so don't freak out. It is the original copy alone which contains the latest copy of the notebook. – demongolem Sep 15 '20 at 21:03
  • 1
    As @mirekphd it's a pity that a new notebook cannot be created, If you have no other notebooks in the folder in which you run jupyter, then moving another notebook from a different folder and opening it saved my day. – Jose M Gonzalez Dec 02 '20 at 16:37
  • It works like a charm! I used to agonizingly close all the opened notebooks and restart jupyter to solve this problem. Now, I am not afraid of this error anymore. – Eren Apr 19 '21 at 06:40
  • I literally did "Print to PDF" from Chrome to try and save my work, this answer saved me some scary thoughts. :D – Smitty Jul 15 '21 at 15:10
  • I reopened the same notebook in another tab... saw it at way previous state. Tried to save it and it asked me if I want to overwrite...so closed it. And the message in the original notebook was gone. And I was able to access the variables etc... V V helpful. – user1953366 Apr 14 '22 at 18:13
  • Your solution helped me overcome this challenge. I had to open another existing jupyter notebook, and the issue disappeared for good. thanks a lot! – Birasafab Jul 16 '22 at 16:11
  • It worked, but I'm just curious. Does anyone know what causes the problem to occur and why this is the solution? – Austin Wolff Oct 10 '22 at 13:47
  • Posting this comment to note that I encountered the same error running JupyterLab v4.0.5 in Brave v1.57.47 (Chromium: 116.0.5845.96) and the advice in this answer didn't work for me, but the advice in [this comment](https://stackoverflow.com/questions/55014094/jupyter-notebook-not-saving-xsrf-argument-missing-from-post#comment96787435_55014094) (opening the notebook in a new browser window) did. – iff_or Aug 25 '23 at 01:59
189

The solution I came across seems too simple but it worked. Go to the /tree aka Jupyter home page and refresh the browser. Worked.

GDB
  • 3,379
  • 2
  • 26
  • 38
  • 18
    This worked for me whereas the solution to open another notebook failed in my case. – DISC-O Apr 20 '20 at 20:19
  • 4
    Thank goodness I can see which posts I've upvoted so I know which solution to use when I forget and come back in a year. – eric Oct 22 '20 at 18:49
  • 1
    The accepted answer did not work for me, but this did (although I am using Jupyter Lab in case this matters). I didn't dare to refresh my Jupyter Lab (running at mydomain.sth/lab?), because it might have lost my changes, so I opened mydomain.sth/tree as suggested here, refreshed and it worked. Thanks! – Verena Haunschmid Mar 31 '21 at 07:30
  • The accepted answer did not work for me, but this did. I am not using Jupyter Lab but Jupyter Notebook with a conda-based kernel. – yellowjacket05 Jan 31 '22 at 00:47
  • 1
    The accepted answer did not work for me, but this did – doraemon Nov 17 '22 at 09:08
20

The only solution worked for me was:

  1. I opened a new tab in chrome
  2. I pasted : http://localhost:8888/?token=......
  3. then I went to my original notebook and I was able to save it
salconte
  • 213
  • 2
  • 5
10

I use jupyter notebooks daily and had never experienced this issue before... until today. I had the notebook open all day but it wasn't running anything and then for no apparent reason stopped auto-saving with the '_xsrf' argument missing from POST error message in the top right. FYI - this is a python3 notebook.

I don't know the cause of this problem but I have recently upgraded my python3 version to 3.7.2 and upgraded all of my site-packages to their latest version as of a few days ago which could possibly be the cause.

As for a solution, as suggested in the comment by @AlexK, I opened the same notebook in a new window (different browser in fact), using

jupyter notebook list

in the terminal to get the URL with login token.

This resulted in me having the notebook open and savable again but the information I had entered since the last successful auto-save was missing. Thankfully, my broken instance was still open and working apart from saving so I was able to simply copy and paste the information across then hit save. So, keep the broken instance open if you try this!

Biggsy
  • 1,306
  • 1
  • 12
  • 28
  • 3
    After you open the second tab, you should be able to go back to the first and save, so you don't even have to copy and paste to the new one. At least, that's how it worked out for me. – DataOrc May 21 '21 at 17:18
9

Simply refreshing the tree tab worked for me.

thomaskeefe
  • 1,900
  • 18
  • 19
8

You can disable the XSRF checks by launching e.g. JupyterLab with:

jupyter lab --ServerApp.disable_check_xsrf=True

This probably means that your server is not token-authenticated. For instance, you may have launched JupyterLab with:

jupyter lab --NotebookApp.token='' --NotebookApp.password=''

And it also probably means that you are running JupyterLab on you local machine and your server is not accessible remotely, so this is fine.

But in general beware that if your server doesn't have authentication, it is vulnerable to XSRF.

David Brochart
  • 825
  • 9
  • 17
5

When I click 'save' button, it has this error. Based on the answers in this post and other websites, I just found the solution. My jupyter notebook is installed from pip. So I access it by typing 'jupyter notebook' in the windows command line.

(1) open a new command window, then open a new jupyter notebook. try to save again in the old notebook, this time ,the error is 'fail: forbidden'

(2) Then in the old notebook, click 'download as', it will pop out a new windows ask you the token.

enter image description here

(3) open another command window, then open another jupyter notebook, type 'jupyter notebook list' copy the code after 'token=' and before :: to the box you just saw. You can save this time. If it fails, you can try another token in the list

music_piano
  • 638
  • 7
  • 9
  • This did it for me, thanks! Just wanted to clarify the last step - it took me a minute to decipher what you meant. The command `jupyter notebook list` will spit out a long url, part of which is a long string of characters appearing in between `token=` and `::`. Copy and paste this string of characters into the "password or token" box from step (2) and the "forbidden" error in the original jupyter notebook will go away. – Jorge Aurelio Menendez Jun 28 '19 at 07:16
5

In my case, this problem was solved by clicking 'Kernel' (shown on the top of notebooks) and then 'Reconnect'.

Note Added: In some versions of Jupyter, there is not 'Reconnect'.

Toru Kikuchi
  • 332
  • 4
  • 4
  • This didn't work for me. It reconnected the Kernel but the `'_xsrf' argument missing...` error remained. – Bill Nov 26 '21 at 22:07
5

I also came across the same error. I just opened another non-running Juputer notebook and an error is automatically gone.

3

1 workaround to solve this problem is:

  1. Download the notebook which you are not able to save by going to: File --> Download as --> Notebook (ipynb).

  2. Open the downloaded notebook by clicking on the downloaded file on the jupyter browser UI

Now, you should be able to save (or rename) this notebook from jupyter UI

Saurabh Verma
  • 6,328
  • 12
  • 52
  • 84
  • 1
    Opening another notebook didn't solve it for me, but this did (i.e. downloading the nb) – Alex Jul 19 '20 at 10:59
3

The error was gone just after refreshing the tab of my browser.

AnksG
  • 488
  • 4
  • 9
  • 2
    This is a bit risky because you could lose your work. I would at least File > Download as > ipynb or File> Make a Copy first. – Sherman May 06 '22 at 17:07
2

I was able to solve it by clicking on the "Kernel" drop down menu and choosing "Interrupt."

1

I got the same problem (impossible to save either notebooks and .py modules) using an image in the nvidia docker. The solution was just opening a terminal inside jupyter without typing anything but exit once the files were saved. It was done in the same browser/jupyter instance.

Machine OS: Ubuntu 18.04

brz
  • 11
  • 2
1

What worked for me was just changing the kernel.
To change the kernel just go to the top-page bar of the notebook, select Kernel, then Change kernel, and then select another Kernel among the ones you have (you need to have at least two preset kernels, obviously). After changing it the message '_xsrf' argument missing from post disappeared and the notebook could be saved again.

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 29 '22 at 02:17
1

I believe there are enough solutions posted to solve the problem. But just in case someone wants to know why this error arises:

The "XSRF argument missing from POST" error is typically encountered when you are trying to make a POST request to a Jupyter notebook server, and the server is unable to verify that the request is legitimate.

This error can occur when the request is missing the required XSRF (Cross-Site Request Forgery) token. This token is a security measure that helps to protect against malicious attacks by ensuring that requests to the server are legitimate.

marinovik
  • 286
  • 4
  • 12
0

Open the developer setting and click console and type the following

JSON.parse(document.getElementById('jupyter-config-data').textContent).token

Then try saving the Notebook. The notebook that was not saving previously will save now.

0

The most voted answer doesn't seem to work when using Jupyter Lab. This one does, however. Just copy the url into a new tab, replace 'lab' with 'tree' and hit enter to load the page. It will generate a new csrf token for your session and you're good to go!

I would suggest enabling Settings > Autosave Documents by default to avoid worrying about losing work in future. It saves very regularly so everything should be up to date before any timeouts happen anyway.

I did not need to open a new notebook. Instead, I reopened the tree, and reconnected the kernel. At some point I also restarted the kernel. – user650654 Oct 9 '19 at 0:17

EdemaRuh
  • 111
  • 1
  • 6
0

In My Case, I have a close tab of Home Page. After Re-opening the Jupyter.The Error was automatically gone and We can save the file.

Dharman
  • 30,962
  • 25
  • 85
  • 135
0

For anyone using jupyterlab on AWS Sagemaker - duplicating the tab is enough to solve the issue.

Idodo
  • 1,322
  • 11
  • 18
0

The rest of the answers didn't work for me. I went to localhost:8888 and clicked on running, then shut down the notebook from there. Then I swapped back to the original notebook and clicked "don't restart". After that save the notebook and restart it.

Big Dumb
  • 120
  • 1
  • 7
-1

This is the easiest way.

I did not need to open a new notebook. Instead, I reopened the tree, and reconnected the kernel. At some point I also restarted the kernel. – user650654 Oct 9 '19 at 0:17

liangli
  • 1,151
  • 9
  • 11