Can't try common method to prevent (google) colab from disconnecting - timeout

Common fix to google colab fails
Google colab times out after 30-90 mins instead of 12 hours, and trying
function ConnectButton(){
console.log("Working");
document.querySelector("#connect").click()
}
setInterval(ConnectButton,60000);
raises errors in the inspector view. Is there a screenshot or different solution to prevent colab from disconnecting? I've already tried this solution and it doesn't work. Thanks for help in advance.

there are many ways to solve this problem, these scripts become obsolete after a while, because google isn't stupid..
I advise you to write a script for example in python that provides to make clicks for you like this:
import numpy as np
import time
import mouse
while True:
random_row = np.random.random_sample()*100
random_col = np.random.random_sample()*10
random_time = np.random.random_sample()*np.random.random_sample() * 100
mouse.wheel(1000)
mouse.wheel(-1000)
mouse.move(random_row, random_col, absolute=False, duration=0.2)
mouse.move(-random_row, -random_col, absolute=False, duration = 0.2)
mouse.LEFT
time.sleep(random_time)
or you can generate a new cell using this javascript script like this:
function ClickConnect(){
console.log("Working");
document.querySelector("colab-toolbar-button").click()
} setInterval(ClickConnect,60000)

Related

xarray holoviews (hvplot.xarray) issue in Spyder

I have some .nc data that I use xarray to analyze. I wanted to overlay a shapefile on the plot and came across hv.plot which seems to have the interactive component I have been needing. However, when I tried to plot, it does not seem to work. Then I found the following thread: Why doesn't holoviews show histogram in Spyder? but could not get Maxime's answer to work and Sander's only partly works, however I have trouble "connecting to the local host" consistently. Does anyone have a solution?
ds_path = 'Z:/MODIS-LAADS-DAAC/'
ds_file = 'anom_2016_v1.nc'
ds_anom = xr.open_dataset(ds_path+ds_file)
x = ds_anom.Rrs_667.isel(time=0)
# create holoviews plot
hv_map = x.hvplot()
# display graph in browser
# a bokeh server is automatically started
bokeh_server = pn.Row(hv_map).show(port=12345)
# stop the bokeh server (when needed)
bokeh_server.stop()
However, I get this 95% of the time ERR_CONNECTION_REFUSED but occasionally get it to work and look like this (however I don't know why!!!) sample ocean data plot
IN SUMMARY:
What I want to do:
https://tutorial.xarray.dev/scipy-tutorial/04_plotting_and_visualization.html#interactive-bokeh-plots-using-hvplot
, https://hvplot.holoviz.org/user_guide/Geographic_Data.html#declaring-an-output-projection
What I have tried:
https://discourse.holoviz.org/t/whats-the-most-efficient-way-to-overlay-a-shapefile-onto-a-hvplot-xarray-plot/397, Why doesn't holoviews show histogram in Spyder?
What I get: ERR_CONNECTION_REFUSED but occasionally (don't know why) sample ocean data plot

This document requires 'TrustedScriptURL' assignment in Google Sheets

I have a Google Spreadsheet where I have the following information on specific cells in the sheet:
Cell B1: Has the URL http://www.google.com.co/search?q=NASA+watching+now%3A+site%3Awww.youtube.com
Cell B2: has the following formula: =IMPORTXML(B1,"//title")
Here is the link of the Google spreadsheet - if you want to test from your side.
And here is the Google Spreadsheet I'm working on - which, I want to get the specific data:
Title: Text (in the h3 HTML tag of the result item).
Url: Link (in the <a> HTML tag of the result item)
Description: Text next to the thumbnail of the result item.
See screenshot with the data to get using IMPORTXML:
The previous code returns the title of the given URL - in this case, the URL stored in the B1 cell.
It was working without problems (since 12/02/2022 - dd/MM/yyyy) until today (13/02/2022 - dd/mm/yyyy).
I checked the Chrome console "F12 Developer tools" and I get this error:
This document requires 'TrustedScript' assignment.
injectIntoContentWindow # VM364:27
By clicking the # VM364:27 line, the following code is shown:
function injectIntoContentWindow(contentWindow)
{
if (contentWindow && !injectedFramesHas(contentWindow))
{
injectedFramesAdd(contentWindow);
try
{
contentWindow[eventName] = checkRequest;
contentWindow.eval( /* ERROR with and (X) is shown here. */
"(" + injectedToString() + ")('" + eventName + "', true);"
);
delete contentWindow[eventName];
}
catch (e) {}
}
}
Searching on the internet, I barely could get the causes of this error:
Google Chrome update - making security stricter.
Chrome extensions - try to disable such extensions and try again.
CPS (Content-Security-Policy) - must be honest = I don't understand this point; it's from the website to scrape the data OR from Google Sheets the CPS is the root cause?
The solutions given to this problem are in Python - with the use of DOMPurify - as is described in this answer, but, I don't know and neither have found any clues about this problem and its solution in Google Spreadsheets.
I've tried:
Recover previous working Google Spreadsheet version - the result is that the formula re-evaluates and no result is returned; looking in Console, the This document requires 'TrustedScriptURL' assignment message shows.
Disable Google Chrome installed extensions - I only have AdBlock (this code was working without issues), anyway, I turned off, reload the spreadsheet and the error mentioned above raises in Console.
Using another page - I tried with Wikipedia and Wiki.fandom and it works - i.e. data is returned. Probably in this case, Google injected in their searcher an script for avoid injection? - I'm really not sure, only speculating/rambling here.
Another possible cause I consider is maybe my IP was blacklisted or blocked, but, I'm not sure.
My browser information:
Chrome version: 98.0.4758.82 (Build oficial) (64 bits) (cohort: Stable)
Windows 10 Version 21H2 (Build 19044.1466)
Is there any way to solve this error in Google Sheets?
PS: I'm interested in know the workaround using google sheets and/or custom scripts - via script editor/Apps Script. The use of IMPORTXML function is not mandatory - I find curious that it was working and then today, not anymore.
I will just leave this here:
=INDEX(UNIQUE(REGEXREPLACE(QUERY(IMPORTXML(A1, "//a/#href"),
"where Col1 contains 'youtube.com/'"), "\/url\?q=|&sa=.*", )))
=IMPORTXML(A1, "//title")
=IMPORTXML(A1, "//h3")
TL;DR: This document requires 'TrustedScript' assignment error is not the root cause of the delay of the IMPORTXML function - probably there is other cause(s) (outside of the developer's handling), but, after all, the code works - see working google spreadsheet - just, wait until the results are shown or use another way to web-scrapping the desired data.
Since the This document requires 'TrustedScript' assignment message stays appearing in the Console of the browser, but, the code I posted on my question (and the code posted by SO user player0 in their answer) works, it seems to me that the delay of the response using IMPORTXML might be due its buggy functionality and/or some restriction Google detected by doing multiple requests.
So, here are my tips about this:
Check very closely how the page where the data will be extracted from and its structured before doing excessive requests - or you might face a significant delay in the response of the IMPORTXML function - as I experienced it.
Get more familiar with XPath and check if the data is dynamically generated in the page - this makes even harder to get the desired data using this way of scrapping.
This is the spreadsheet with the desired results - if anyone is interesed1.
1 Check the "Results-mix" sheet (which contains both the code I manage to create and the code provided by player0 in their answer).
If you really want to get similar results in a less convoluted way, consider use another strategy for web-scrapping or use official APIs - when available.

Are Google API token.json files single use?

I'm making a simple app to access google sheets that I have saved on google drive. I set up a project on google, created the Oauth credentials and ran the Python quickstart code to generate the token.json file.
Yesterday, after doing that, I ran this portion of the quickstart code and it ran perfectly and returned the rows from the sample spreadsheet:
###Add step to pull in previous staff comments, joino in MRN
from __future__ import print_function
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
###This only runs when not connected to NetExtender. Should work when ported to citrix but running locally for testing is going to be difficult
###Gsheets
SCOPES = ['https://www.googleapis.com/auth/drive','https://www.googleapis.com/auth/spreadsheets']
# The ID and range of a sample spreadsheet.
SAMPLE_SPREADSHEET_ID = '1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms'
SAMPLE_RANGE_NAME = 'Class Data!A2:E'
creds = Credentials.from_authorized_user_file('token.json', SCOPES)
service = build('sheets', 'v4', credentials=creds)
# Call the Sheets API
sheet = service.spreadsheets()
result = sheet.values().get(spreadsheetId=SAMPLE_SPREADSHEET_ID,
range=SAMPLE_RANGE_NAME).execute()
values = result.get('values', [])
if not values:
print('No data found.')
else:
print('Name, Major:')
for row in values:
# Print columns A and E, which correspond to indices 0 and 4.
print('%s, %s' % (row[0], row[4]))
However, today when I run that code, it doesn't work anymore. I get an error:
('invalid_scope: Bad Request', {'error': 'invalid_scope', 'error_description':'Bad Request'})
Are the token files single use and would I need to generate a new one every time I want to run this (that's the only reason I can imagine it would work fine yesterday but not today)? If that is the issue, is there a way to program this so that I don't need to re-authenticate in Google and create a new token file every time I want to run this?
Thanks!

FastAI Question on data loading using TextList

My end-goal to implement ULMFit using FastAI to predict disaster tweets(as a part of this Kaggle competition). What I'm trying to do is read the tweets from a Dataframe. But for reasons unknown to me, I'm stuck at the data loading stage. I'm simply unable to do so using the below method -
from fastai.text.all import *
train= pd.read_csv('../input/nlp-getting-started/train.csv')
dls_lm = (TextList.from_df(path,train,cols='text',is_lm=True)
.split_by_rand_pct(0.1)
#.label_for_lm()
.databunch(bs=64))
This line throws - NameError: name 'TextList' is not defined.
I'm able to work around this problem with the below code -
dls_lm = DataBlock(
blocks=TextBlock.from_df('text', is_lm=True),
get_x=ColReader('text'),
splitter=RandomSplitter(0.1)
# using only 10% of entire comments data for validation inorder to learn more
)
dls_lm = dls_lm.dataloaders(train, bs=64, seq_len=72)
Why does this work and not the previous method?
Notebook Link for reference.
Which version of fastai are you running?
import fastai
print(fastai.__version__)
TextList class is from FastAI v1, but it seems to me your import path is for Fastai v2, and in v2, TextList is changed with https://docs.fast.ai/text.data.html#TextBlock (thats why it's working with the Datablock part wich is the good way to handle this)

PEPPER (SoftBank Robotics): ALSpeechRecognition Engine issue - How to restart it when it doesnìt work?

During my test on Pepper, I found some difficulties in realizing continuative collaborative dialog.
In particular, after about 10 minutes, it seems that the ALSpeechRecognition engine stops working.
In other words, Pepper dialog panel remains empty and/or the robot does not understand my words, even if the structure worked some minute before.
I tried to stop and restart it (i.e., the engine) via SSH terminal, by using:
qicli call ALSpeechRecognition.pause 1
qicli call ALSpeechRecognition.pause 0
It should restart the engine according to the guidelines shown here, but it does not work.
Thank you so much guys.
Sincerely,
Giovanni
According to the tutorial, starting and stopping the speech recognition engine is done by subscribing/unsubscribing it.
The recommended way to do this is unsubscribing and subscribing back to it. For me it also worked changing the speech reco language and chaging it back to the one you had previously.
Luis is right and to do so just create a function as below given and call it if ActiveListenning event comes false from ALSpeechRecognition module. Note: Use ALMemory module to get data from ALSpeechRecogntion.
asr_service = ALProxy("ALSpeechRecognition",ip,port)
memory = ALProxy("ALMemory",ip,port)
def reset():
asr_service.unsubscribe("ASR_Engine")
asr_service.subscribe("ASR_Engine")
ALS = memory.getData("ALSpeechRecognition/ActiveListening")
if ALS==False:
reset()

Resources