Using Python To Quickly Open Multiple Websites In Your Browser

Generated with AI ∙ January 18, 2024 at 1:10 p.m.

I had a problem where I was opening about a dozen bookmarks at the same time in Brave, and in doing so, it was giving me a “503 Service Temporarily Unavailable” error.

To fix this, I turned to Copilot Pro with this prompt:

Write a Python program that opens in my web browser (using Django?) that has a list of URLs that it opens in new tabs with a pause of 0.5 seconds between them. For the initial URLs, use Google and Microsoft.

Continue reading “Using Python To Quickly Open Multiple Websites In Your Browser”

How to Save Python Files in the Same Directory in VS Code (Update)

In a recent post I gave a programmatic solution to how to get Python to write files in the same folder as the Python file itself when working in VS Code. By default, Python programs in VS Code write files in the root folder of the project, not in the folder where the Python file is saved.

My solution involved the use of a built-in variable named “__file__“, which together with some fancy path concatenation, achieved what I was hoping for.

Continue reading “How to Save Python Files in the Same Directory in VS Code (Update)”

How to Save Python Files in the Same Directory in VS Code

Introduction

Have you ever been frustrated because your Python programs in VS Code always save in the root folder of your GitHub project, even if the program is in a subfolder? You’re not alone. I’ve encountered this problem too, and I couldn’t find a solution within VS Code’s settings.

Solution

The solution is within Python itself.

Python has a built-in variable called __file__ that refers to the path of the current Python file. By using this with os.path.join(), you can ensure that your file will be saved in the same directory as your program, not in the root folder.

The os.path.join() function in Python is a smart way to stick together pieces of a file path. It knows how to correctly use the right kind of slash (/ or \) depending on your computer’s operating system. So, if you have a folder named “folder” and a file named “file.txt”, os.path.join("folder", "file.txt") will give you the correct full path: “folder/file.txt”. It’s a handy tool for dealing with file paths in Python.

Continue reading “How to Save Python Files in the Same Directory in VS Code”

Ten Things I Learned – December 5, 2023 Update

April Wine

I was sad to hear of Myles Goodwyn‘s death. Myles was the lead singer of April Wine, a Canadian band that had quite an influence on my musical tastes.

WordPress

Want to your open WordPress links in a new tab as the default? “External Links in New Window / New Tab” is your solution.

Speed Test

Google has a speed test webpage. (I’ve added it to my Google A-Z page.)

Continue reading “Ten Things I Learned – December 5, 2023 Update”

Quick Encryption/Decryption Utility

lock on keyboard

Need a quick program to encrypt or decrypt small amounts of text, such as passwords? Here’s a quick utility I wrote in Python using Google Colab, Google’s implementation of Jupyter Notebooks. You can run the program there or copy it to your own Google Drive first. Note that you’ll need a Google account to access it.

https://bit.ly/beens-encrypt

I personally use this utility to encrypt sensitive information in Google Keep, as shown in the screenshot below. I save the URL of the utility with the encrypted text so I can decrypt it quickly.

(For longer notes, I’d honestly recommend a proper password manager that can encrypt notes. I use the Premium version of Bitwarden, which I highly recommend.)

The instructions in the program should be adequate to run it on your own.

Bookmark it in your browser for quick access! (Ctrl-D on your PC or Command-D on your Mac)

Python f-strings: Use of “=” (very cool!)

In Python f-strings, the equal sign (=) is used to print both the variable name and its value in a string. It is used to facilitate print-debugging by expanding to the text of the expression, an equal sign, then the representation of the evaluated expression. For example, if you have a variable named x with a value of 5, you can use an f-string like this: f'{x=}' which will expand to 'x=5'⁷.

Try it out here!

Source: Conversation with Bing, 3/30/2023

(1) python – What does = (equal) do in f-strings inside the expression …. https://stackoverflow.com/questions/59661904/what-does-equal-do-in-f-strings-inside-the-expression-curly-brackets Accessed 3/30/2023.

(2) Python f-String Tutorial – String Formatting in Python Explained with …. https://www.freecodecamp.org/news/python-f-strings-tutorial-how-to-use-f-strings-for-string-formatting/ Accessed 3/30/2023.

(3) Python f-strings: Everything you need to know! • datagy. https://datagy.io/python-f-strings/ Accessed 3/30/2023.

(4) Python 3’s f-Strings: An Improved String Formatting Syntax (Guide). https://realpython.com/python-f-strings/ Accessed 3/30/2023.

(5) PEP 498 – Literal String Interpolation | peps.python.org. https://peps.python.org/pep-0498/ Accessed 3/30/2023.

(6) Modulo String Formatting in Python – Real Python. https://realpython.com/python-modulo-string-formatting/ Accessed 3/30/2023.

(7) f-strings in Python – GeeksforGeeks. https://www.geeksforgeeks.org/formatted-string-literals-f-strings-python/ Accessed 3/30/2023.

(8) What does ‘f’ mean before a string in Python? – Stack Overflow. https://stackoverflow.com/questions/58233423/what-does-f-mean-before-a-string-in-python Accessed 3/30/2023.

This post was inspired by this tweet:

Converting Lists of Data to CSV — Using a Colab Notebook Hosted in Gists

I’ve really been enjoying learning about data analysis lately. Part of what’s made it so enjoyable is the use of Colab, which is Google’s version of Jupyter Notebook.

I came across a need to convert some lists of data to a CSV (comma separated values) format so I could paste it into a Google Sheet. Not finding anything online to do I decided to write my own, and here is the product.

The Colab notebook I created has been saved as a “gist”, which is GitHub’s cousin for fast and easy file storage service. The Gist website is also very popular for sharing CSV files, which this search will attest to.

You’ll notice an “Open in Colab” button at the top, which is how you will open the document. A really nice feature is that it is fully usable without needing to save the file anywhere, but for those that do want to save their work, they can, into Google Drive.

When you first run it, you’ll get a scary message that was written by Google’s lawyers. You’ll see from the source code that the notebook’s not doing anything nefarious, so just trust me and click on “Run Anyway”. 😆

Google Colab Warning

This is a preview of the notebook. To open it, click here or on the filename on the bottom left corner of the preview.

Some Python programmers may want to remind me that I could have used Python’s built-in CSV library, but I felt Pandas would be faster, especially for large lists.

I hope some of you find this useful, although I’ll be happy if it gets some of you interested in Colab for Python programming or for data analysis, or even if it just introduces you to the Gist website.

Integrating Google Drive into Google Colab Notebooks

Why might we want to integrate Google Drive into Google Colab? The primary reason for this would be to easily access data files to use in your Colab notebooks. 

Here is a video that outlines the basic process. For written instructions, see below.

Integrating Google Drive into Google Colab is quite simple. The first step is to go to Google Colab, then open a new notebook.

Continue reading “Integrating Google Drive into Google Colab Notebooks”