Python and surrounding subjects.

Jupyter

Install kernels

  • Installation:
virtualenv <dir>
source <dir>/bin/activate
pip3 install ipython ipykernel
python3 -m ipykernel install --user --name=<kernel-name>

Remove kernels

jupyter kernelspec list
jupyter kernelspec uninstall <kernel-name>
  • Scala kernel

Almond.sh

docker run -it --rm -p 8888:8888 almondsh/almond:latest
docker run -it --rm -v $(pwd):/home/jovyan/mydirectory --name almond -p 8888:8888 almondsh/almond:latest

Jupyter - set host IP

It helped with using jupyter with VPNs at times.

jupyter lab --ip=127.0.0.1

Autoreload

Reloads external functions on the fly.

%load_ext autoreload
%autoreload 2

Widgets (ipywidgets)

Example on github

Google Collab - Handle disconnecting

Tricks and tips

    function ConnectButton(){
        console.log("Connect pushed");
        document.querySelector("#top-toolbar > colab-connect-button").shadowRoot.querySelector("#connect").click()
    }
    setInterval(ConnectButton,60000);

Numpy

https://www.dataquest.io/blog/numpy-cheat-sheet/

Pandas

Official Pdf on Github

# Fixing group-by things:
c_counts.groupby("cited").sum().reset_index()

# Handle duplicates
df.drop_duplicates()
df[df.duplicated("column")]

# Rearrange columns
df = df[["colx", "coly", "colz"]]

Mongo

Connect to pymongo (replica or not) Using direct connection

client = MongoClient(f"mongodb://{mongo_server}", read_preference=ReadPreference.SECONDARY, directConnection=True)