Command Line to Open .py with IPython - jupyter-notebook

I would like to use the great IPython Web Interface to open, evaluate, edit and save the following "myfile.py" (see below) avoiding the annoying process: Create an .ipynb > import myfile.py to it > make some evaluation or edition > export to .py > remove unnecessary code lines and finaly get again the following content (myfile.py):
import os
# <codecell>
# Number division
print(4/5)
# Number Plus
print(1+40)
Is there a command line to do so?
Notes:
I want work ONLY with .py files, any solution with store/work with .ipynb (JSON files) not be welcome.
Suggestions for other programs will be very welcome.

On stable version, use the --script flag, it always save .py file wihthout having to go through the export process. Still it also save the .ipynb file along side.
On dev version there are now pre-save hook that allows you to do whatever you want for saving .
To automatically load .py files, you will have to write your own Notebook File Loader backend that accept .py files.

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

%run command to run .ipynb file forces .py extension

I am trying to execute a simple run command:
%run /WaterfallViz.ipynb
But no matter how I format it I keep getting the same error:
ERROR:root:File `'/WaterfallViz.ipynb.py'` not found.
How do I prevent the .py extension to insert itself at the end of the file name?
If you have moved the notebook file where you %run another notebook, unfortunately Jupyter will still reference to the original directory path.
To fix this behavior, simply:
save your notebook
create a new notebook
copy the content from the original notebook
open the new notebook file
The %run now should work as the "directory path" is the one you are expecting, and the .ipynb file will be picked up instead of trying to open a .py file too.
I was having the same issue and I found out other ways to import the notebook that avoid this error, without needing to follow all the process recommended before (creating a new notebook, etc).
You need to first install a library:
pip install import-ipynb
Then, import it in your notebook:
import import_ipynb
And finally you can import your file:
import WaterfallViz

How to specify where cronR saves output file

I have successfully run a simple cronR tutorial using the cronR add in within R Studio. Here is the following code that I have saved in a R script file:
library(glue)
current_time <- Sys.time()
print(current_time)
msg <- glue::glue("This is a test I am running at {current_time}.")
cat(msg, file = "test.txt")
The R script file is saved within a specific project directory. The log file when the job runs is also saved there. However, the output of the script file, test.txt, is saved in my home directory. I am on a Mac. In the tutorial, it was stated that this would happen, that cron would save any output in the home directory and that if I want to change the location that I have to "specify otherwise". However, the tutorial gives no instructions for how to do this and I am not sure if I am supposed to do this through the terminal in mac and if so how? Changing the file path in the script file (e.g. Documents/test.txt) changes nothing, as the test.txt file is still saved in the home drive. I suspect I have to make this change somewhere else but I am not sure where. Any help would be appreciated.
For anyone who runs into the same issue, I was able to solve my problem by using launchD. I followed this tutorial https://babichmorrowc.github.io/post/launchd-jobs/. It worked as anticipated and now my .txt file is saved to the correct place. There is also a tutorial there for cron jobs, though if you are on a mac, launchD is apparently the preferred method.

In Bluesky Statistics How do I write output to a csv file

I can't get write.csv or write.table to work in the r editor of BlueSky Statistics.
I usually just use this format in RStudio and it works perfectly.
write.csv(df, "zzz.csv")
Any hints?"
The default install location for BlueSky Statistics is 'C:\Program Files', where by default, there is no write permission (for creating or deleting files). Also, saving a file in the install location is not safe, as the file may get lost/deleted when the application is uninstalled. So it is always good to save your file(s) in your own folder(s) where you also have write permission.
In short, try to provide a writable location/path in write.csv() or other similar functions/commands.
See example below:
To save your file to the Desktop folder.
write.csv(df, "C:/Users/<YourUsername>/Desktop/zzz.csv")
Note: use forward slash(/) as a path separator.

How to create .jl file in IJulia

I am very new to julia. I have just installed ipython and Ijulia. But every time I saved my file from ipython notebook, the format of the saved file is always .ipynb. I don't know if I can saved my file as .jl file. Or could anyone tell me how to create .jl file through ipython notebook. I have googled but seems like no one talks about it.
.ipynb is the JSON-based Jupyter notebook format, with conventions for storing code cells and associated metadata and data (such as inline images). Jupyter is designed as a fully-integrated interactive environment, not "just" a text editor, and as such the file format requires extra information. To create a .jl file, use a text editor or an ide such as Juno.

Resources