Julia: how do I configure IJulia kernel to use specific environment? - julia

I downloaded someone else's project and the structure is as follows:
project/
notebooks/
notebook_a.ipynb
notebook_b.ipynb
library/
Manifest.toml
Project.toml
src/
test/
In the notebooks I would like to import library, and install its dependencies within its own evironment.
Following the sugestions here, I can do
using Pkg
Pkg.activate("../library/")
but I wonder if I could install a kernel that has the project directory specified and automatically activates the library environment. What should I pass to IJulia's installkernel? "--project=..." what?
I use conda regularly and this confused me because I thought that running notebook once the environment is activated in the command line would have the right environment in the notebook, but this was not the case.

IJulia starts its own Julia process and hence is not using environment settings from its master.
By default IJulia sets the environment (Project.toml) from the folder it was started. The most convenient thing would be to move the notebooks folder to be subfolder of library and then just run:
notebook(dir="/path/to/project/library")
If you do not want to change the folder structure you still need to run:
notebook(dir="/path/to/project/notebooks")
Once in the notebook you need to run:
using Pkg
pkg"activate /path/to/project/library"

Related

is it possible to create new virtual environment in Julia using environment file (like .yml)?

is there a way to setup a virtual environment in Julia using an environment file? (for instance like .yml file for creating conda venv)
Julia Version: 1.7.1
OS: Windows 10
In Julia virtual environment is defined via Project.toml file (that keeps package names and their acceptable versions) and Manifest.toml (that keeps the exact dependence tree and package versions generated along requirements defined in Project.toml).
Here is a sample Julia session:
julia> using Pkg
julia> pkg"generate MyProject"
Generating project MyProject:
MyProject/Project.toml
MyProject/src/MyProject.jl
julia> cd("MyProject")
julia> pkg"activate ."
Activating environment at `/home/ubuntu/MyProject/Project.toml`
Finally, note that you can manipulate the Project.toml by eg. adding a package like this (this assumes the environment is active):
pkg"add DataFrames"
Sometimes you want to provide package version information to your Project.toml, for an example you could add at the end of the file:
[compat]
DataFrames = "1.3.0"
After adding the first dependency the Mainifest.toml file has been generated. Copying this file together with Project.toml across machines allows you to duplicate the environment.
In order to have all packages installed on a new machine you will need to run:
pkg"activate ."
pkg"instatiate"
pkg"instatiate" can also be used to generate Mainfest.toml when only the Project.toml is present.
The nice thing is that Julia can store many package versions simultaneously and the virtual environments only links to the central package repository (contrary to Python that copies several GBs of data each time).
pkg provides this with project.toml files.
open julia in console
julia>]
Pkg> activate <Name>
<Name> Pkg> add <PackageName>
create directory with

How to change Jupyter start-up folder for different conda environments

After seeing this post on how to set the start-up folder for Jupyter Notebooks, I looked for how to do so for specific conda environments and haven't found an answer.
Is there a way to open up a Jupyter notebook in a location that is different depending on which conda environment within which you're activating it? I'm looking for a solution like the one above, where I could change c.NotebookApp.notebook_dir = '/the/path/to/home/folder/', but in some environment-specific config file.
I guess an alternative would be to set some macro to activate the environment, cd to the desired folder location for this environment, then run jupyter notebook from that location.
I was able to generate a DOSKEY macro to do the job. I combined this answer which shows how to set persistent aliases (macros) in command prompt, with this answer which shows how to use multiple separate commands in a DOSKEY macro. As a summary here (mostly from Argyll's answer in the above persistent macro/DOSKEY post):
Create a file called something like alias.cmd
Insert the macro to automatically activate a conda environment, change file locations, and run a jupyter notebook from that location:
doskey start_myEnv = conda activate myEnv $T cd C:\Users\user\path\to\my\notebooks\ $T jupyter notebook
Run regedit and go to HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor
or HKEY_CURRENT_USER\Software\Microsoft\Command Processor if not on Windows 10.
Add a String entry with the name AutoRun with the value set as the full path to the alias.cmd file.
Anytime you open the command prompt, executing start_myEnv will now activate myEnv, change to the folder that relates to that environment, and start a jupyter notebook.

Why does Binder not recognize a requirements.txt file?

I am attempting to load a Jupyter Notebook through mybinder.org. The repository that the Notebook is in includes a requirements.txt file with the content
parsl
because the Notebook uses the parsl library.
When I load the notebook and import the library, execution stops with the error
ModuleNotFoundError: No module named 'parsl'
The requirements.txt file is right there. I have no idea what else to do to get mybinder.org to load the module. What am I missing?
Without seeing the repo you are referring to, one suggestion would be you don't have your requirements.txt where it needs to be. If it isn't in the root directory of the repository, it has to be in a folder named binder, see here.
There is a repo that has parsl working. You can fork the repo from here, or alternatively you use https://github.com/Parsl/parsl-tutorial as a basis to make your own and then add in your notebooks.

Correct workflow? - Distributable environment including jupyter notebook

I am developing applications that use Jupyter notebook and ipywidgets for a GUI frontend to a backend codebase. I have run into issues distributing/installing packages in the normal way, such as:
unexpected differences between required library versions (e.g. pandas)
requirements.txt forcing an update to more recent version of a library when the user maintains and uses their own codebase on an older version of that library.
I think pipenv might be able to solve this problem, but I want to check I have a correct usage before going too far down this path.
Requirements:
the user needs to be able to restart Jupyter Notebook in the same env multiple times, running the program from scratch, until a new version is available.
Users are all on Mac.
Any installation should not alter site-packages etc, have no effect on the python setup any user currently has.
Workflow concept
Development:
Develop within a pipenv environment (I use Pycharm so this is relatively straightforward).
Include jupyter in Pipfile [requires], even though jupyter is not imported anywhere in my source.
Use pipenv install new_package as and when new packages are required by my codebase, and maintain the Pipfile (respecting --dev for testing packages etc).
User installation
Produce a zip file containing source code, setup.py etc plus Pipfile and Pipfile.lock.
User extracts the zip file to a known location on their machine.
In terminal, navigate to the unzipped folder location, and run pipenv install.
Use:
In terminal, navigate to the folder location, and run pipenv shell
Run pipenv run jupyter notebook to reload the env and notebook.
When finished, close out of notebook and run exit to close the env.
Uninstall env and upgrade to newer version
In terminal, navigate to the folder location, and run pipenv --rm.
Download new source zip and follow steps above.
If I've understood, this should ensure anyone can use the distribution in a tightly controlled environment, without making any alterations to their existing python install? Have I overcomplicated things?

Jupyter notebook custom.css in created environment

Does anyone know how to set custom.css in for a conda environment?
I created a new environment using
conda create -n myenv
But it doesn't use the same custom.css I created in the default environment.
Any idea where I should put the custom.css file to get Jupyter to use it?
I'm using Windows 7.
See the docs here for info on where paths are located by default (for conda you'd probably be looking at the {sys.prefix}/etc/jupyter path).
Or you could just run jupyter --paths in your conda environment shell to find out where it is looking for custom files.

Resources