How to convert a python project to ipynb format notebook? - python

I have a python machine learning project in a folder. I want to change the whole project into ipynb formate notebook. Actually I want it in Google Colab.

You can do the following to achieve your goal in easiest way:
Compress your folder to a zip file
Open google colab and create a new notebook
On the left side there will be a menu, click the upload button on the upper left corner of that menu and upload the folder.
User ' !unzip /path-to-file' to unzip the whole folder.
Ps: you can copy the zipped folder path by right clicking on it and it will give you a few options including 'copy path'.

Use p2j to convert Python source code to Jupyter Notebook.
From the command line, run
pip install p2j
then go to the directory where your file is located. -->( for example-> cd downloads, if the file is in download directory)
then run
p2j myscript.py
This will create a myscript.ipynb file.

Related

How to config automatic sync Jupyter notebook .ipynb and .py files in VSCode e.g. by using Jupytext

I have written some Jupyter notebooks using original Jupyter notebook web interface. All notebooks are synced nicely in this way.
But now, I would like to edit my notebooks in the VSCode. But I cannot configure syncing notebook file with its python script.
I tried this using jupytext:
created file jupytext in the folder ~/.config
put the next code into this file:
# Always pair ipynb notebooks to py:percent files
default_jupytext_formats = "ipynb,py:percent"
But no effect!
(Update) Can this be achieved, as a first solution, using VSCode Tasks (I am not used tasks yet)?
May be it possible to run the task with jupytext command if the notebook file is opened/saved/modified?
Currently, VSCode does not support such a function. The Jupyter function in VSCode is provided by a Python extension, which supports us to convert between .ipynb files and .py files in VSCode.
.ipynb files to .py files : Export as python script.
.py files to .ipynb files : Right click, "Export Current Python File as Jupyter Notebook"
I have submitted the requirement you described, and we look forward to the realization of this feature. Giuhub link: How to synchronize the jupyter file and python file of VSCode.

Google CoLab - How to run a jupyter notebook file that is in the 'Files' tab (i.e. /content/) of my CoLab environment

In Google CoLab on the left is a pane that can be opened that shows Table of Contents, Code snippets, and Files.
In the Files pane there is an upload button, I can upload a notebook file to this Files area. But once the notebook file is uploaded, there is no option to run it as a notebook. The menu option File->OpenNotebook doesn't show the CoLab /content/ files as an option to start a notebook.
Is there a way to do this? Or can it be added in future releases?
The reason for this request is I'd like to git-clone a repo with multiple notebook files into the /content (or Files) area of CoLab. And then be able to easily switch between the notebooks, much like the native Jupyter notebook interface that shows a directory with potentially multiple notebooks that can be started.
I've tried right-clicking on the notebook file in Files but there is no option to start the notebook. I've tried using File->Open_notebook... the Files files aren't shown as an option in any of the tabs.
The desired results is that I can start .ipynb files (i.e. Jupyter notebooks) directly from the 'Files' or /content/ section of Google CoLab.
You can run other notebooks in your current notebook like this:
# if the file was on the google drive
%run /content/gdrive/My\ Drive/Colab\ Notebooks/DenseVideoArchitecture.ipynb
# simply replace the path in your case
%run /content/DenseVideoArchitecture.ipynb
But what you are asking is to switch between different notebooks in the same environment which might not be possible in collab.
I couldn't understand what you actually need, but I hope below code help you:
from google.colab import drive
drive.mount('/content/gdrive')
!cd content/gdrive/My Drive/Colab Notebooks
You should mount your google drive and now you have access to drive as a local drive. In this code, at first two lines, I mount gdrive and then I redirect to some place in google drive for example "Colab Notebooks" and you can run everything you want.

Uploading files to Jupyter running on VM VitualBox

Is it possible to upload a file (say a .pdf or .csv) saved in my hard disk into Jupyter running on VirtualBox?
Yes, it's possible.
To begin with, your host PC mut have a shared folder with your VM (Explained elsewhere).
A method I do is to
First create a folder myfolder for the (.pdf or .csv) files.
Secondly, load the folder by: going to the created myfolder --> right-click on the myfolder --> choose "Open in Terminal".
Thirdly, at the terminal it should look like this: myfolder]$. Then I type "jupyter notebook" as such: [username#localhost myfolder]$ jupyter notebook and hit ENTER.
The folder should be opened and containing my .pdf or .csv files.

Jupyter -- opening notebooks on mac

This is a bit hard to explain so bear with me! Suppose I have a Jupyter notebook in some directory. The only way I know to open it is to click repeatedly through the web based directory structure. Is there a nice way to do this using the mac finder. In other words I would like a simple dialogue box to open from Jupyter to navigate to my notebook.
edit: thanks, I realized that I can start in certain directories, but what if I'm opening a bunch of different notebooks from different directories. It sure would be nice to be able to jump to my favorites easily.
As #GiantsLoveDeathMetal mentions... if you navigate to a specific directory in your terminal and then type jupyter notebook the notebook will open and display in your browser, just the content of that folder.
For example:
$ cd /path/to/the/folder/with/your/notebook.ipynb
$ jupyter notebook

How to change working directory in Jupyter Notebook?

I couldn't find a place for me to change the working directory in Jupyter Notebook, so I couldn't use the pd.read_csv method to read in a specific csv document.
Is there any way to make it? FYI, I'm using Python3.5.1 currently.
Thanks!
Running os.chdir(NEW_PATH) will change the working directory.
import os
os.getcwd()
Out[2]:
'/tmp'
In [3]:
os.chdir('/')
In [4]:
os.getcwd()
Out[4]:
'/'
In [ ]:
You may use jupyter magic command as below
%cd "C:\abc\xyz\"
First 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\Python Projects.
Then on that line, paste it like this : c.NotebookApp.notebook_dir = 'C:\\Users\\username\\Python Projects'
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.
on Jupyter notebook, try this:
pwd #this shows the current directory
if this is not the directory you like and you would like to change, try this:
import os
os.chdir ('THIS SHOULD BE YOUR DESIRED DIRECTORY')
Then try pwd again to see if the directory is what you want.
It works for me.
it is similar to jason lee as he mentioned earlier:
in Jupyter notebook, you can access the current working directory by
pwd()
or by import OS from library and running os.getcwd()
i.e. for example
In[ ]: import os
os.getcwd( )
out[ ]: :c\\users\\admin\\Desktop\\python
(#This is my working directory)
Changing Working Directory
For changing the Working Directory (much more similar to current W.d just you need to change from os.getcwd() to os.chdir('desired location')
In[ ]: import os
os.chdir('c:user/chethan/Desktop') (#This is where i want to update my w.d,
like that choose your desired location)
out[ ]: 'c:user\\chethan\\Desktop'
It's simple, every time you open Jupyter Notebook and you are in your current work directory, open the Terminal in the near top right corner position where create new Python file in. The terminal in Jupyter will appear in the new tab.
Type command cd <your new work directory> and enter, and then type Jupyter Notebook in that terminal, a new Jupyter Notebook will appear in the new tab with your new work directory.
Jupyter under the WinPython environment has a batch file in the scripts folder called:
make_working_directory_be_not_winpython.bat
You need to edit the following line in it:
echo WINPYWORKDIR = %%HOMEDRIVE%%%%HOMEPATH%%\Documents\WinPython%%WINPYVER%%\Notebooks>>"%winpython_ini%"
replacing the Documents\WinPython%%WINPYVER%%\Notebooks part with your folder address.
Notice that the %%HOMEDRIVE%%%%HOMEPATH%%\ part will identify the root and user folders (i.e. C:\Users\your_name\) which will allow you to point different WinPython installations on separate computers to the same cloud storage folder (e.g. OneDrive), accessing and working with the same files from different machines. I find that very useful.
Open jupyter notebook click upper right corner new and select terminal then type cd + your desired working path and press enter this will change your dir. It worked for me
list all magic command %lsmagic
show current directory %pwd
I have did it on windows machine. Detail mentioned below
From windows start menu open “Anaconda Prompt
Find .jupyter folder file path .
In command prompt just type
or to find the .jupyter path
After find the .jupyter folder, check there has “jupyter_notebook_config” file or not. If it is not there then run below command
After run the command it will create "jupyter_notebook_config.py"
if do not have administrator permission then Some time you could not find .jupyter folder . Still you can open config file from any of the text editor
Open “jupyter_notebook_config.py” file from the the “.jypyter” folder.
After open the file need to update the directory is use for notebooks and kernel. There are so many line in config file so find “#c.NotebookApp.notebook_dir” and update the path
After:
Save the file
6. Now try to create or read some file from the location you set
What works for me on Windows is creating a shortcut to jupyterlab, and altering the properties of the shortcut.
At "Start in:", enter your desired working directory.
Using this method, you don't have to configure a global file either.

Categories

Resources