Archive of posts about Category: Coding

Setting your working directory to Google Drive in a Colab notebook

There are plenty of articles detailing how to mount your Google Drive to a Google Colab notebook. It’s simple, you just run this in your notebook:

from google.colab import drivedrive.mount('/content/drive', force_remount=True)

But what if you want your notebook to use a folder in your Google Drive as the working directory?

That way, any files that are created within your project will automatically be saved in your Google Drive. If your project runs 12 hours and then times out due to inactivity, any files or checkpoints created will be in your Google Drive.

I wrote a quick script to do this. Just change the project_folder to the folder you want to use. The script will create the project_folder from scratch if it doesn’t exist and then set it to the current working directory.

It also creates a sample text file in that directory to verify that it’s working.

You can run !pwd in a notebook cell at any time to verify your current working directory.

import os # Set your working directory to a folder in your Google Drive. This way, if your notebook times out, # your files will be saved in your Google Drive! # the base Google Drive directory root_dir = "/content/drive/My Drive/" # choose where you want your project files to be saved project_folder = "Colab Notebooks/My Project Folder/" def create_and_set_working_directory(project_folder): # check if your project folder exists. if not, it will be created. if os.path.isdir(root_dir + project_folder) == False: os.mkdir(root_dir + project_folder) print(root_dir + project_folder + ' did not exist but was created.') # change the OS to use your project folder as the working directory os.chdir(root_dir + project_folder) # create a test file to make sure it shows up in the right place !touch 'new_file_in_working_directory.txt' print('\nYour working directory was changed to ' + root_dir + project_folder + \ "\n\nAn empty text file was created there. You can also run !pwd to confirm the current working directory." ) create_and_set_working_directory(project_folder) ...continue reading...

Collecting ActiveCampaign Subscriber Traffic Source Data

On my way to learning JavaScript, I’ve been alternating between tutorials and small projects. I try to pick projects that will stretch my skills without being so ambitious that I end up giving up or have to learn five new technologies just to get them working. ...continue reading...

My first Ruby on Rails Project

I spent the week between Christmas and New Year’s building my first Rails app with a lot of help from the amazing Rails Tutorial. I’m a big fan of Michael Hartl’s approach to technical sophistication (googling stuff, being comfortable with not understanding everything you read in the code, and just being resourceful enough to get stuff done). ...continue reading...

Google Tag Manager and Turbolinks in Ruby on Rails

I’m pretty new to Ruby on Rails (and coding in general), but I finally got my first little app up and running and so naturally I wanted to add Google Tag Manager to it so I could track traffic to it.

Turns out that the Turbolinks gem doesn’t play well with GTM. I googled around for it and everyone was pointing to this article, which I was going to try as a last resort because I’m just not a fan of virtual pageviews and it just seems like their should be a way to get Turbolinks to fire JS when you tell it to.

I read the Turbolinks docs and found this:

Running JavaScript When a Page Loads

You may be used to installing JavaScript behavior in response to the window.onload, DOMContentLoaded, or jQuery ready events. With Turbolinks, these events will fire only in response to the initial page load—not after any subsequent page changes.

In many cases, you can simply adjust your code to listen for the `turbolinks:load` event, which fires once on the initial page load and again after every Turbolinks visit.

javascriptdocument.addEventListener("turbolinks:load", function() {// ...})
I used that snippet to modify my GTM `<script>` tag so it looks like this:

<script> document.addEventListener("turbolinks:load", function() {
(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start': new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0], j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src= 'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f); })(window,document,'script','dataLayer','GTM-XXXXXXX');
console.log("GTM fired"); }) </script> ...continue reading...

Extracting URL strings into Google Tag Manager

I’ve been teaching myself JavaScript in the last few weeks to up my GTM implementation skills, so I thought I’d share some examples of how I’ve used it. ...continue reading...

<< | >>