How to read google spreadsheet using google colab - google-sheets

We can list out spreadsheet present in google drive using below command
from google.colab import drive
drive.mount('/content/drive')
!ls -l /content/drive/'Shared drives'
but unable to read spreadsheet using below command
import gspread
from oauth2client.client import GoogleCredentials
gc = gspread.authorize(GoogleCredentials.get_application_default())
gc.open('/content/drive/'Shared drives/data.gsheet').data available
and also one more problem we have space in sheetname(data available) and we don't have access to change sheetname
I have refer link:: https://colab.research.google.com/notebooks/io.ipynb
Kindly help on it.

You can use gc.open_by_url('gsheets_url') to open the document (no need to mount the drive). For the sheet name, you can use gsheets.worksheet('sheet name').
So on your case it'd go something like:
from google.colab import auth
auth.authenticate_user()
import gspread
from oauth2client.client import GoogleCredentials
# setup
gc = gspread.authorize(GoogleCredentials.get_application_default())
# read data and put it in a dataframe
gsheets = gc.open_by_url('your-link')
sheets = gsheets.worksheet('data available').get_all_values()
df = pd.DataFrame(sheets[1:], columns=sheets[0])
(credits to this post)

Update to the answer by Murilo Cunha, as it gives errors for authentication
from google.colab import auth
auth.authenticate_user()
import gspread
from google.auth import default
creds, _ = default()
gc = gspread.authorize(creds)
import pandas as pd
# read data and put it in a dataframe
gsheets = gc.open_by_url('Your link')
sheets = gsheets.worksheet('data available').get_all_values()
df = pd.DataFrame(sheets[1:], columns=sheets[0])

It's helpful if you could say what is the exact error you get when you run this script. However, I notice there are redundant quotes in following script,
gc.open('/content/drive/'Shared drives/data.gsheet')
replace extra above by following,
gc.open('/content/drive/Shared drives/data.gsheet')
Another option is to try gc.open_by_url() instead of gc.open()

Related

Find files in google colab

The following snippet in my google colab:
import sys
import os
from google.colab import drive
drive.mount('/content/gdrive')
path = '/content/gdrive/MyDrive/MBA/MBA_USP/03_Modulo_Solucoes/16_Metologia_Pesquisa_Projeto_Conclusao/TCC'
fnames = os.listdir(path)
prints my files and directories:
['TCC', 'degree_dist.eps', 'mind_dataset']
Now I'd like to search for files starting from the above path, like so:
if os.path.exists('mind_dataset/file.csv'):
try:
# do something
But this is not finding file.csv, which is there. How do I append or join paths to my present path in colab, in order to find any file?

How to use this library in python "from digidevice import location"?

How can I get coordinates from user's device, using this library in python: "from digidevice import location".
I tried, but cant even import this library.
This should help: link
It seems like this will work:
from digidevice import location
loc = location.Location()
print(loc.position)

fastai show_batch and show_results does nothing

I build a model on fastai v2.3.1. When I try to call functions show_batch and show_results it does not show anything. Here is the problematic code:
from fastai.vision.all import *
from fastai.data.all import *
import fastai.vision
import zipfile as zf
import random
import timeit
fields = DataBlock(blocks=(ImageBlock, CategoryBlock),
get_items=get_image_files,
get_y=yer,
splitter=RandomSplitter(valid_pct=0.2, seed=random.randint(0, 10)),
item_tfms=RandomResizedCrop(224, min_scale=0.5),
batch_tfms=aug_transforms()
)
dls = fields.dataloaders(os.path.join(Path(os.getcwd()), "train"), num_workers=0, bs=32)
dls.show_batch()
learn = cnn_learner(dls, resnet18, metrics=error_rate)
learn.fine_tune(2)
learn.show_results()
I can use model but these functions.
I ran into the same thing, found the answer here:
https://www.debuggingtissue.com/latest-articles/how-to-save-fastai-plots-when-using-an-external-server-or-terminal-environment
Basically PyPlot is creating a graphics object but not displaying it, so you need to immediately tell plt to save/show the buffer.
So it's as easy as typing "plt.show()" after the show_results() call!
import matplotlib.pyplot as plt
...
learn.show_results()
plt.show()
(Took me forever to find that out, hope this helps!)

How to import custom modules in google colab? (Solution given here not working)

I'm trying to import a custom module called 'clusterer.py' into my colab notebook. But the import function is not able to find the file. Is there any other way to import custom modules?
After mounting the drive, I have already tried this approach: How to import custom modules in google colab? with the result: 'No module named 'clusterer''
!ls /content/gdrive/My\ Drive/Colab\ Notebooks/Omdena_Mars_Anomaly_Detection/*.py
!cat /content/gdrive/My\ Drive/Colab\ Notebooks/Omdena_Mars_Anomaly_Detection/clusterer.py
import sys
sys.path.append('/content/gdrive/My\ Drive/Colab\ Notebooks/Omdena_Mars_Anomaly_Detection')
import clusterer
The output is as follows:
'/content/gdrive/My Drive/Colab Notebooks/Omdena_Mars_Anomaly_Detection/clusterer.py'
'/content/gdrive/My Drive/Colab Notebooks/Omdena_Mars_Anomaly_Detection/feature_extractor.py'
[Contents of the module 'clusterer.py']
ModuleNotFoundError: No module named 'clusterer'
Ok, I think I figured out one solution. In my case, I had to physically go inside the directory where the files were. This is what I did
1a. Restart the kernel but 'resetting all runtimes', specially if you've just added your file.py to the directory.
1b.cd gdrive/My Drive/Colab Notebooks/Omdena_Mars_Anomaly_Detection
!ls /content/gdrive/My\ Drive/Colab\Notebooks/Omdena_Mars_Anomaly_Detection/*.py
!cat /content/gdrive/My\ Drive/Colab\ Notebooks/Omdena_Mars_Anomaly_Detection/mylib.py
import sys
sys.path.append('/content/gdrive/My\ Drive/Colab\ Notebooks/Omdena_Mars_Anomaly_Detection')
5.import clusterer
Worked for me.
Thanks

how to create exe of Python Code using opencv with or without py2exe?

i have some Python Code working properly its some kinda simple thing using opencv.
for example
import cv2
x = cv2.imread('Dog6.jpg')
cv2.imwrite('new2.jpg')
setup.py is
from distutils.core import setup
import py2exe
import cv2
setup(console=['name.py'])
but i am unable to create exe of this code with py2exe.
is there another method to create exe of such program ?
Error is
ImportError:numpy.core.multiarray failed to import
Put the line:
import numpy
Into your script, i.e the first file and try again.

Resources