Skip to content

Instantly share code, notes, and snippets.

View korakot's full-sized avatar

Korakot Chaovavanich korakot

  • Bangkok, Thailand
View GitHub Profile
@korakot
korakot / secret.py
Created May 9, 2024 07:25
Google colab secret key userdata
from google.colab import userdata
key = userdata.get('MY_APY_KEY')
@korakot
korakot / exit.py
Last active April 17, 2024 01:30
Terminate kernel to restart google colab
print("Runtime is now restarting...")
print("You can ignore the error message [Your session crashed for an unknown reason.]")
sleep(0.5)
os._exit(0) # restart
@korakot
korakot / record.py
Last active April 7, 2024 14:35
Record audio in Colab using getUserMedia({ audio: true })
# all imports
from IPython.display import Javascript
from google.colab import output
from base64 import b64decode
from io import BytesIO
!pip -q install pydub
from pydub import AudioSegment
RECORD = """
const sleep = time => new Promise(resolve => setTimeout(resolve, time))
@korakot
korakot / sheet_df.py
Last active March 29, 2024 23:33
Save dataframe to Google Sheet from Colab
# authenticate
from google.colab import auth
auth.authenticate_user()
import gspread
from oauth2client.client import GoogleCredentials as GC
gc = gspread.authorize(GC.get_application_default())
# create, and save df
from gspread_dataframe import set_with_dataframe
title = 'New Sheet'
gc.create(title) # if not exist
@korakot
korakot / selenium.py
Last active March 26, 2024 07:40
Use selenium in Colab
# install chromium, its driver, and selenium
!apt update
!apt install libu2f-udev libvulkan1
!wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
!dpkg -i google-chrome-stable_current_amd64.deb
!wget https://edgedl.me.gvt1.com/edgedl/chrome/chrome-for-testing/118.0.5993.70/linux64/chromedriver-linux64.zip
!unzip -j chromedriver-linux64.zip chromedriver-linux64/chromedriver -d /usr/local/bin/
!pip install selenium chromedriver_autoinstaller
# set options to be headless, ..
@korakot
korakot / unzip.py
Last active March 26, 2024 07:29
Unzip a file in the current directory (-j ignore path info)
!unzip -j archive.zip path/to/file.txt
!unzip -j archive.zip path/to/file.txt -d /to/path/
# download and unzip in one line, quietly
!wget -qO- http://host.com/file.zip | jar x
!curl -s http://host.com/file.zip | jar x
@korakot
korakot / draw.py
Last active March 22, 2024 05:51
Drawing on Google Colab
from IPython.display import HTML, Image
from google.colab.output import eval_js
from base64 import b64decode
canvas_html = """
<canvas width=%d height=%d></canvas>
<button>Finish</button>
<script>
var canvas = document.querySelector('canvas')
var ctx = canvas.getContext('2d')
@korakot
korakot / plugin_domains.txt
Last active February 29, 2024 07:25
Chatgpt plugin domain list
dmtoolkit.magejosh.repl.co
seo-plugin.orrenprunckun.com
talkfpl.beegreeeen.workers.dev
videohighlight.com
aiplugin-experiences.owlting.com
www.nani.ooo
jetbook.click
imageeditor.dev
api.speedybrand.io
c-resume.copilot.us
@korakot
korakot / suppress.py
Created February 19, 2024 09:27
Suppress stdout
import os
import sys
from contextlib import contextmanager
@contextmanager
def suppress_stdout():
with open(os.devnull, 'w') as devnull:
original_stdout = sys.stdout
sys.stdout = devnull
try:
@korakot
korakot / clear.py
Last active February 16, 2024 07:11
Clear cell output in Colab
from google.colab import output
output.clear()
# IPython also has clear_output() but this is shorter.
from IPython.display import clear_output