How to change working directory in Julia Jupyter? - jupyter-notebook

How to change working directory in Julia Jupyter?
I tried and read everything, still have no idea how to do that. It only allows me to select directories under my home ~/ dir. I can't find any button to go up to /.

I'm pretty sure once you have started the server you cannot then go up directories, I may be wrong though.
So best thing to do is start the jupyter notebook server somewhere that contains all of the folders you might need -i.e. the root dir if you want to make sure you have access to everything.
You can use the --notebook-dir flag for this. Or you can set defaults in the config.

you need to create the config file, using cmd :
jupyter notebook --generate -config
Then, search for C:\Users\your_username\.jupyter folder (Search for that folder), and right click edit the jupyter_notebook_config.py.
Then, Ctrl+F: #c.NotebookApp.notebook_dir ='' . Note that the quotes are single quotes. Select your directory you want to have as home for your jupyter, and copy it with Ctrl+C, for example: C:\Users\username\JuliaProjects.
Then on that line, paste it like this : c.NotebookApp.notebook_dir = 'C:\\Users\\username\\JuliaProjects'
Make sure to remove #, as it is as comment.
Make sure to double slash \\ on each name of your path.
Ctrl+S to save the config.py file !!!
Go back to your cmd and run jupyter notebook. It should be in your directory of choice. Test it by making a folder and watch your directory from your computer.

I use Jupyter Lab and start it from the Julia REPL (1.4) like this:
using IJulia
jupyterlab(dir=pwd(), detached=true)

Related

Set Jupyter Lab working directory to the directory where Jupyter was started, not the parent directory of the notebook? [duplicate]

Question: How can one set the working directory of all notebooks opened in Jupyter Lab with a double-click, to be the project's folder, /myproject/, regardless of the notebook's subfolder within that parent folder? The working directory is identified by !pwd on Linux/Mac or !cd on Windows.
Context:
The Jupyter Lab sessions is initiated from the project's folder, by: /myproject/jupyter lab.
I am not looking for changing the working directory within the notebook with code (e.g. with !cd.. or using os), but change the settings of Jupyter Lab, such that the all kernels will start with the folder from where Jupyter Lab was initiated.
This is useful for being able to use consistent relative folders, regardless of the subfolder they are referred from. For example, for loading data in a specific subfolder.
I agree that this is often a preferred approach! I always have my notebooks configured to work like that because it:
makes it easy to specify paths to data and for outputs and
allows moving a notebook between directories without the need to change the paths
makes jupyterlab-lsp code intelligence work more reliably
I have a python module called make_paths_absolute.py with the following contents:
from os import chdir
from pathlib import Path
def make_paths_relative_to_root():
"""Always use the same, absolute (relative to root) paths
which makes moving the notebooks around easier.
"""
top_level = Path(__file__).parent
chdir(top_level)
make_paths_relative_to_root()
And in the first cell of every notebook, I add a line import make_paths_absolute. I like it this way because it makes it:
reproducible: anyone who copies/clones my project will be able to run the code without the need to customize anything in their Jupyter environment
work with all notebook interfaces (whether JupyterLab/RetroLab/classic Notebook)
is quite obvious for anyone reading the notebook that they should expect the paths to be absolute (=relative to the root).
To make that work you first need to set PYTHONPATH pointing to the root of the repository when starting JupyterLab. To do so on Linux/Mac you can prepend the jupyter lab command with:
PYTHONPATH=/path/to/your/lab/root:$PYTHONPATH jupyter lab
Alternatively, you can specify PYTHONPATH in kernel.json (either by modifying the default kernel specification or creating a copy of it first, see https://stackoverflow.com/a/53595397).
PS. The above is a simplification of my setup. In reality, I store make_paths_absolute.py in helpers package (so there is also an empty helpers/__init__.py and there is extra .parent) together with other initialization-time code, including a notebook called notebook_setup.ipynb which contains things like %matplotlib inline, pandas customizations (like making sure it uses stable sort), warning filters etc. So my every notebook starts with:
import helpers.make_paths_absolute
%run helpers/notebook_setup.ipynb
and I am really liking this setup working like that for two years now without any problems.
There is a feature request for making this easier at: https://github.com/jupyterlab/jupyterlab/issues/11619.
The most easiest solution in my opinion
Open Notepad
Paste the command "jupyter notebook --NotebookApp.iopub_data_rate_limit=1.0e10"
Save the notepad file with an extension of ".bat" instead of ".txt"
Paste the file in which directory you want to initialize your jupyter
Double click and open the ".bat" file
Jupyter opens with desired directory as base
This way you control the jupyter root directory as and when required and don't
really have to perform any manual settings
Hope this helps
".bat" file created on Desktop
enter image description here
".bat" file double clicked and executed
enter image description here
jupyter opens with Desktop as the intended base directory
enter image description here

How to set default folder in JupyterLab file browser?

I was wondering if there is a way to set a default folder every time I open JupyterLab. Is there any command I can run through Anaconda PowerShell Prompt? Or a JSON property I can modify in settings tab? Thanks in advance!
If you want to open specific file in directory a, say a/notebook.ipynb but then navigate the file browser to directory b, relative to the root directory, you can use file-browser-path query in the navigation URL (documented here):
http(s)://<server:port>/<lab-location>/lab/tree/a/notebook.ipynb?file-browser-path=/b
You can use this method from command line thanks to LabApp.default_url traitlet:
jupyter-lab --LabApp.default_url='/lab/tree/a/notebook.ipynb?file-browser-path=/b'
You can skip the opening specific file part:
jupyter-lab --LabApp.default_url='/lab?file-browser-path=/b'
If you want to change the root directory, please see this answer which explains how to do so with ServerApp.root_dir traitlet (in older versions of Notebook<7.0 and JupyterLab<3.0 used to be named NotebookApp.notebook_dir). Again, you can use it from command line, and even specify an absolute path:
jupyter-lab --ServerApp.root_dir='/home/user/project/'
# or on Windows say:
# jupyter-lab --ServerApp.root_dir='C/users/user/project/'
You can combine the two approaches, e.g.:
jupyter-lab --ServerApp.root_dir='/home/user/project/' --LabApp.default_url='/lab?file-browser-path=/project_subdirectory'
If you want to persist the changes (for either default URL or root dir), see the previously linked answer which details how to persist configuration.
Follow the answer . Just edit the alias alias my_jupn='source /home/$USER/anaconda3/bin/activate && conda activate MyPy38 && jupyter-notebook --notebook-dir=/home/$USER/Documents/Jupyter_Notebook'
and for JupyterLab alias my_jupl='source /home/$USER/anaconda3/bin/activate && conda activate MyPy38 && jupyter-lab --notebook-dir=/home/$USER/Documents/Jupyter_Notebook'
N.B. Here MyPy38 is your active environment name. and if any error occurs then change $USER to your UserName. Lastly, /home/$USER/Documents/Jupyter_Notebook will be change according to your choice.
According to the jupyter.org:
I configured like this.
Terminal command active your envirnment by conda activate Your_env_name and then jupyter lab --generate-config. jupyter_lab_config.py should be created in dir /home/$USER/.jupyter directory.
Find
# c.LabApp.app_settings_dir = '' and uncomment.
Then give your directory like this.
c.LabApp.app_settings_dir = '/home/$USER/Documents/Jupyter_Notebook'
Similar for jupyter notebook. jupyter notebook --generate-config and then nu-comment and edit like c.NotebookApp.notebook_dir = '/home/UserName/Documents/Jupyter_Notebook'
**NB: /home/$USER/Documents/Jupyter_Notebook is my changed default dir. Change it according to your need. **
The command
jupyter notebook --generate-config
will create a config file.
The path to this file is displayed in your terminal or PowerShell.
Set the desired folder path as the string c.ServerApp.root_dir in this file and make sure you uncomment the line after you set the string.
Windows users will have to use double backslash in paths, e.g.:
c.ServerApp.root_dir = 'C:\\YOUR\\PATH'

Rstudio Server Run from specific directory

I am spinning up an instance of rstudio server and I need the working directory of R to be a specific directory. I would also like the file pane in the bottom right corner to be pointing to the same directory. Is there a way to do this? Currently it runs from the home directory of whichever user is running the program. I have tried the --server-working-dir flag, and it does not seem to work. Here is the command I am using:
/usr/lib/rstudio-server/bin/rserver \
--server-daemonize=0 \
--server-user=user \
--server-working-dir=/some/path \
--auth-none=1 \
--auth-minimum-user-id=0
Any help would be useful here.
[edit] Just wanted to clarify that I would like the server to start in this directory. I am building a container that will be deployed multiple times, and I don't want the users to have to set their directories every time it is deployed.
If you want to modify the file pane in the right, you should edit /etc/rstudio/rsession.conf.
And add two lines in below:
session-default-working-dir=/some/path
session-default-new-project-dir=/some/path
You can do this by edditing the (global) R profile startup script. Here's a step by step guide:
1) Run Rscript -e "R.home()" -- this will tell you the location of your R directory home. In my case (Mac) it is /Library/Frameworks/R.framework/Resources
2) Go to /Library/Frameworks/R.framework/Resources/etc -- e.g., $R_HOME/etc
3) sudo touch Rprofile.site if it doesn't exist, then sudo nano Rprofile.site
4) Add the following lines and save:
cat("hi\n")
setwd("/some/path/")
You should avoid overwriting the users home directory.
Amongs the [.Rprofile] files you should only edit the Rprofile.site as a last resort since it acts globally.
Suggested solution:
R read the "initialization file" at start, in the following order:
.Rprofile.site
.Rprofile (located in the current directory).
.Rprofile (in the users home directory).
In your case if you are planning to login to R-Studio server you will end up in the users home directory, so I would suggest you to just edit the [.Rprofile] in the home directory. In case the [.Rprofile] is missing you need to create it.
Add this line in your .Rprofile [in your home directory]:
setwd ('/your/path/')
Logout/login to your R-studio server session and you will notice that the "file pane to the right location" has changed accordingly to what you specified in your .Rprofile.

How to save a file into a directory in Jupyter notebook?

I invoke "jupyter notebook" under:
[abigail#localhost anaconda3]$ jupyter notebook
By default, it saves to the directory of anaconda3/ with an extension of "ipynb" when I click "File" => "Save".
How to save it to a directory under anaconda3/, instead of the default location? There is not a "save as" command in notebook?
You can save a notebook to a location of your choice by using the "File" -> "Download as" -> "Notebook (.ipynb)" option from the menu.
Alternatively you can start your notebook server from a different directory and it will save all notebooks to that directory.
A third option is to navigate to the directory you want the notebook to be saved to in the tree view "http://127.0.0.1:8888/tree" prior to creating the notebook.
There are two methods:
1.You can use the magic command %notebook to save as ipynb
%notebook "directory/to/file/filename.ipynb"
2.You can use the magic command %%writefile to save as py file
%%writefile "directory/to/file/filename.py"
In the second method, you should put this command at the top of the cell, otherwise it will throw an error.
It is easiest to select a destination before you create a program using Jupyter Notebook; as then you do not run into this issue.
However, since you have already made a program, one possible solution is to make a copy of the file, move it to your desired location, and then delete the old file. Before doing this, ensure that you have saved it first, otherwise data might be lost.

Update current working directory after vim netrw exit

Here's the scenario.
I am on the unix command line (in home directory). I want to browse the directory through
$ vim .
thus opening the vim netrw.
Now I am browsing the directory using the netrw.
What I want here is that when I exit vim netwr, I want my previous current working directory (in this example the home directory) to now become the directory I was previously in vim netrw.
Example:
step 1. now in home directory
step 2. vim . (thus opening vim netrw)
step 3. go to any directory (~/my/other/folders)
step 4. :q (to exit vim)
step 5. (here, I want my previous directory to now become ~/my/other/folders
any ideas on how to do it? I was thinking of doing something in .vimrc but I dunno how. Been into google search, but found nothing valuable.
A possible solution would be to change the current work directory while in netrw by pressing c, and spawn a new shell from the folder you're in by issuing :shell
So it would look like:
vim .
Navigate to the desired folder...
c
:shell
And there you are in a shell in the current folder you were in netrw.
And when you exit that shell, you fall back to where you were in netrw and can continue using the explorer.
I don't think it's possible at all. Every command executed via system('command') or :!command is executed through a subshell, not through the shell that started Vim so I don't see how you could alter the host shell in any way.
But I smell an XY problem here. What is your goal?
Do you want to be able to execute some commands on the files you just edited and you want to be in their directory? If so, do you know about :sh? :!command?
Do you want a "graphical" file explorer for your shell? If so, do you know vifm? Ranger? Midnight Commander?
To add to Wadih's answer, you can put this in your .vimrc file:
let g:netrw_keepdir=0
This means the working directory will be automatically updated and you don't need to press c each time.
So after navigating to a folder in netrw, all you need to do is do this:
:sh
And this opens the terminal in the current folder.
From the netrw help file:
By default, g:netrw_keepdir is 1. This setting means that the current
directory will not track the browsing directory. (done for backwards
compatibility with v6's file explorer).
Setting g:netrw_keepdir to 0 tells netrw to make vim's current directory
track netrw's browsing directory.

Resources