Skip to content

Instantly share code, notes, and snippets.

@lmzach09
lmzach09 / app.py
Created September 8, 2019 21:59
creating a desktop app window in python with pyqt
class App(QWidget):
def __init__(self):
super().__init__()
## Set main window attributes
self.title = 'Photo Album Viewer'
self.left = 0
self.top = 0
self.width = 800
self.height = 600
self.resizeEvent = lambda e : self.on_main_window_resize(e)
@lmzach09
lmzach09 / app.py
Created September 8, 2019 21:45
pyqt widget class for making a thumbnail selector. a user can select thumbnail images that are imported from a photo album folder.
## Widget for selecting an image in the directory to display
## Makes a vertical scrollable widget with selectable image thumbnails
class ImageFileSelector(QWidget):
def __init__(self, parent=None, album_path='', display_image=None):
QWidget.__init__(self, parent=parent)
self.display_image = display_image
self.grid_layout = QGridLayout(self)
self.grid_layout.setVerticalSpacing(30)
## Get all the image files in the directory
@lmzach09
lmzach09 / app.py
Created September 8, 2019 21:39
pyqt widget class for displaying an image. the image resizes to fill the space when the user resizes the desktop window
## Widget for the single image that is currently on display
class DisplayImage(QWidget):
def __init__(self, parent=None):
QWidget.__init__(self)
self.parent = parent
self.pixmap = QPixmap()
self.label = QLabel(self)
self.assigned_img_full_path = ''
def update_display_image(self, path_to_image=''):
@lmzach09
lmzach09 / app.py
Created September 8, 2019 21:30
pyqt app example snippet for detecting valid image files from file names
DEFAULT_IMAGE_ALBUM_DIRECTORY = './my-album/'
## Check that a file name has a valid image extension for QPixmap
def filename_has_image_extension(filename):
valid_img_extensions = \
['bmp', 'gif', 'jpg', 'jpeg', 'png', 'pbm', 'pgm', 'ppm', 'xbm', 'xpm']
filename = filename.lower()
extension = filename[-3:]
four_char = filename[-4:] ## exclusively for jpeg
if extension in valid_img_extensions or four_char in valid_img_extensions:
@lmzach09
lmzach09 / app.py
Created September 8, 2019 21:26
pyqt app example snippet
import sys
from os import listdir
from os.path import isfile, join
from PyQt5.QtCore import Qt, QSize
from PyQt5.QtWidgets import *
from PyQt5.QtGui import QPixmap
@lmzach09
lmzach09 / whileExample.js
Created September 2, 2019 18:41
An example of a while loop implementation using the async/await pattern. This works in the browser and Node.js. Get the "request" method here https://gist.github.com/lmzach09/38d291edb90f64d338132824a9de35ce#file-request-js
const request = require('./request.js');
const array = [1, 2, 3, 4, 5];
(async function() {
while (array.length > 0) {
// Dequeue the first element in the array
// Note that this modifies the array!
const el = array.shift();
const response = await request({
@lmzach09
lmzach09 / forLoopExample.js
Last active November 16, 2022 03:45
An example of a for loop implementation using the async/await pattern. This works in the browser and Node.js. Get the "request" method here https://gist.github.com/lmzach09/38d291edb90f64d338132824a9de35ce#file-request-js
const request = require('./request.js');
const array = [1, 2, 3, 4, 5];
(async function() {
for (let i = 0; i < array.length; i++) {
const response = await request({
method: 'GET',
hostname: 'httpbin.org',
path: '/get?myArg=' + array[i]
@lmzach09
lmzach09 / forEachWithCallbackExample.js
Last active September 2, 2019 18:36
An example of a for-each loop implementation with callbacks using the async/await pattern. This works in the browser and Node.js. Get the "request" method here https://gist.github.com/lmzach09/38d291edb90f64d338132824a9de35ce#file-request-js
const request = require('./request.js');
function forEachWithCallback(callback) {
const arrayCopy = this;
let index = 0;
const next = () => {
index++;
if (arrayCopy.length > 0) {
callback(arrayCopy.shift(), index, next);
}
@lmzach09
lmzach09 / requirements.txt
Created September 1, 2019 17:23
simple python 3 chatbot dependencies
requests==2.22.0
lxml==4.4.1
google==2.0.2
beautifulsoup4==4.8.0
@lmzach09
lmzach09 / app.js
Created September 1, 2019 17:18
JS for a ChatBot web page
const submitButton = document.getElementById('submitButton');
const chatbotInput = document.getElementById('chatbotInput');
const chatbotOutput = document.getElementById('chatbotOutput');
submitButton.onclick = userSubmitEventHandler;
chatbotInput.onkeyup = userSubmitEventHandler;
function userSubmitEventHandler(event) {
if (
(event.keyCode && event.keyCode === 13) ||