how to open a jupyter notebook under a folder in google drive? - python

I am trying to open a existing jupyter notebook from a folder in google drive. In colab, I do:
File-> open notbook-> google drive
Only jupyter notebook files under colab Notebooks are shown, even I have a folder inside Colab Notebooks, that folder is not shown. How can I open the notebook inside a folder?
But if I do below code, I can see all the files under content/drive/MyDrive. But I can only see notebook as a text, see below screenshot for the opened notebook shown on the right hand side. how can I open it a jupyter notebook in colab so I can run the cells? Thanks
# Load the Drive helper and mount
from google.colab import drive
drive.mount('/content/drive')

I usually open these in Colab by right clicking directly in Google drive (not from within Colab itself) and selecting Open with --> Google Colaboratory.

Related

run regular python project (.py) in google colab as notebook (.ipynb)

A project I built with python, I have code in python with (.py) scripts in a folder called "MY_PROJECT", is it possible to run the code in the "MY_PROJECT" folder, in google colab?
I want to run my code in google colab, but it is not in notebook format (.ipynb).
I copied my project to Drive, but I don't see a way to run in colab, Google colab search only for notebook format.
Is it possible to do this, to run a python project in colab?

Creating Multiple .ipynb Notebooks in Google Colab

is it possible to create more than the one default notebook in Google Colab Pro?
I first tried to create a new file with the .ipynb file extension and it shows up like this:
Then I tried to create a new notebook like this:
And that file opens up the same way as just creating an new file:
Basically, I'd like to create multiple notebooks like creating an new .ipynb notebook in Jupyter notebooks so I can use Google Colab the same way as Jupyter notebooks. Is this possible in Google Colab?
Google Colaboratory is quite different from jupyter notebook, you can't create and use 2 notebooks in a single session as it would also hurt the resources of the google colab. They do not have made this feature. So it is not possible yet.

Automatically mount and authorise Google Drive in Collab [duplicate]

I'm looking for a way to automate the authentication process when connecting a colab-session to my google drive.
I'd prefer to use the built-in tools for this one, instead of PyDrive.
In short: have the following cell run without having to manually authenticate by logging in and copying the password from the dialogue
from google.colab import drive
drive.mount('/content/drive/')
Automatically mounting to your Drive files is now supported for Colab notebooks which aren't shared by multiple people.
To enable this for a notebook, create a new Drive notebook, open the file browser, and click the 'Mount Drive' button.
You'll see a permissions dialog like so:
After you complete the permissions once, you'll see your Drive mounted in the file browser.
Better still, if you reload the notebook later and reconnect, your Drive will mount automatically with no more drive.mount copy/paste required. Your Drive files will just be there.

How to use Google Colab local runtime and connect to Google Drive

I am running Google Colab with a local runtime. I use this command to start Jupyter:
jupyter notebook --NotebookApp.allow_origin='https://colab.research.google.com' --port=8888 --NotebookApp.port_retries=0
When I previously connected I was able to mount Google Drive with this code in the notebook:
from google.colab import drive
drive.mount('/content/drive')
I was able to connect to Google Drive and access files while on local runtime.
However, now I am getting this error:
ModuleNotFoundError: No module named 'google.colab'
I see other people have this problem, and some suggest using PyDrive. But I am certain I was connected to Google Drive without using PyDrive.
I suspect the first command I ran to start Jupyter was different when Google Drive was able to connect.
Is there a specific flag I have to add to that first command
The google.colab libraries only exist on managed backends.
When running on your local machine, you'll want to instead use Drive's existing sync apps available here: https://www.google.com/drive/download/

CoLab Accessing Files

It is great that I can run jupyter notebooks in CoLab, but I am going crazy saving and loading files. For example, I am writing an assignment for my course and I include figures in it using the HTML tag. (I want to use HTML instead of markdown images so I can set the width.) So in a Text cell I have
<img src="CoLab04.png" width="250">
This works fine when I run the jupyter notebook on my laptop, but in CoLab, it can't find the image even when the image is in the same CoLab folder as the ipynb file. Err.
I have similar problems saving data files. On my laptop I can use the normal python functions open, write, close, etc. That code runs without complaint, but the files do not show up on Google Drive. Not in the CoLab folder or any other folder when I search all of my Google Drive. Err. I read TFM and use
from google.colab import drive, files
drive.mount('/content/gdrive')
fig.savefig("LED12.png") # saves a figure as a file
files.download("LED12.png")
This downloads a file to my laptop. Then I have to upload the file to a Google Drive folder so my students can see it.
Am I missing something? Why is it so hard to create and read Google Drive files using a Google-CoLab jupyter notebook?
I've read https://colab.research.google.com/notebooks/io.ipynb, but why is it so hard? I need something easy for novice students to use. If reading and writing files is this hard, I will have to recommend my students install jupyter on their laptops and not use CoLab.
It seems to me a sys.path problem.
After you mount My Drive by the following code
from google.colab import drive
drive.mount('/content/drive/')
then your main Google Drive can be read with
!ls /content/drive/My Drive/
If you have a sub-folder under My Drive that you wish to centralize your colab project, let's say you have projectA folder under your main Google Drive directory. You can add the projectA folder path to sys.path
import sys
sys.path.append("/content/drive/My Drive/projectA")
Then you should be able to save your fig as the same way you used in your local machine root path. The file will be save to your projectA folder where you run your colab code.
fig.savefig("LED12.png")
You should be able to see the file appear there. If this doesn't work, then try using absolute path when doing open, save, close etc. path sensitive operation:
working_path = '/content/drive/My Drive/projectA'
fig.savefig(os.path.join(working_path, "LED12.png"))
It may be simpler to load notebooks from GitHub, wherein image links in the same repository will be loaded more intuitively.
For example, the notebook below loads a set of images bundled in its GitHub repository.
https://colab.research.google.com/github/jakevdp/PythonDataScienceHandbook/blob/master/notebooks/05.01-What-Is-Machine-Learning.ipynb
The markdown reference for the first graph:
![](figures/05.01-classification-1.png)
[figure source in Appendix](06.00-Figure-Code.ipynb#Classification-Example-Figure-1)
This corresponds to the GitHub repo here:
https://github.com/jakevdp/PythonDataScienceHandbook/
Building on this example, a common pattern for bundling data files is to add a !git clone ... command at the top of the notebook to bring in the entire repo in one shot.
The reason this is simpler to accomplish in GitHub than Drive is that GitHub has unified ACLs at a repostiory level, whereas Drive manages ACLs at the file level. So, it would be a bit cumbersome to have a Drive notebook shared publicly that referenced images or other Drive files that were not shared.
I have done this in Colab (reading, training my model and uploading my trained model) some days ago. Let's make it simple.
Please do the following steps. I am trying to cover both(reading csv as well as uploading a file).
Step 1 : Go to your google drive and create a folder: Colab and keep your files inside Colab folder.
Step 2 : Now, install pydrive in Colab jupyter notebook
!pip install pydrive
Step 3 : Run following commands for accessing Google drive File
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth
from oauth2client.client import GoogleCredentials
Step 4 : Mount drive(Here you will get a link in Colab jupyter shell. Click the generated link and verify your google drive(Just copy and paste the generated code) )
from google.colab import drive
drive.mount('/content/drive/')
Step 5 : Authenticate and create the PyDrive client. Here do the same like step 4 (Click the generated link and verify your google drive(Just copy and paste the generated code) )
auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)
Step 6 : To get the file, replace the id with id(your file id) of file you want to access. For me, it was csv file. To get the id, go to share and generate a link. you will find something like : https://drive.google.com/file/d/xxxxxxxxxxxxxx/view?usp=sharing. Put it(xxxxxxxxxxxxxx) on below and do the same, how many files you want to read.
normal_1 = drive.CreateFile({'id':'13AR0sS1pndF0fTxmdjQRv_1Bv5aBNpkT'})
normal_1.GetContentFile('normal_1.csv')
normal_2 = drive.CreateFile({'id':'1Z0DO8M1Qco07kyVoxYSgxXBx6XYGBzJd'})
normal_2.GetContentFile('normal_2.csv')
abnormal = drive.CreateFile({'id':'12zFHDXVjreorRrHHhYrA1n82VQLuawsl'})
abnormal.GetContentFile('abnormal.csv')
Step 7 : Now, you can read those files and load in a dataframe for further use.
normal_1 = pd.read_csv('normal_1.csv', skiprows = np.arange(100, normal_1.shape[0]))
normal_2 = pd.read_csv('normal_2.csv', skiprows = np.arange(100, normal_2.shape[0]))
abnormal = pd.read_csv('abnormal.csv', skiprows = np.arange(50, abnormal.shape[0]))
Step 8 : Save the model to disk after training your model:Use joblib
from sklearn.externals import joblib
filename = 'model.sav'
joblib.dump(clf, filename)
# Upload model to you google drive
model_file = drive.CreateFile({'title' : 'model.sav'})
model_file.SetContentFile('model.sav')
model_file.Upload()
Now, go to your My drive and refresh it. You will finding something "model.sav". For the complete code in jupyter notebook file, you can visit my github link. I hope it will help you to solve your problem.

Categories