Bimpy images cat <<-EOF | python import bimpy import numpy as np from PIL import Image import io import requests img_urls = [ "https://ds604.neocities.org/Public/dinosaur.png", "https://ds604.neocities.org/Public/baboon.jpg" ] def download_image(url): r = requests.get(url, timeout=4.0) if r.status_code != requests.codes.ok: assert False, 'Status code error: {}.'.format(r.status_code) data = io.BytesIO(r.content) return Image.open(data) ctx = bimpy.Context() ctx.init(800,800,"Image") # image = Image.open("/Users/davidsarmaholdings/Dropbox/Public/dinosaur.png") # image = download_image(img_urls[1]) # image = np.asarray(image, dtype=np.uint8) # image = (image * 0.5).round().astype(np.uint8) # im = bimpy.Image(image) im = None while not ctx.should_close(): with ctx: if bimpy.button("RGB image. Cat"): im = bimpy.Image(download_image(img_urls[0])) if bimpy.button("Checkerboard"): im = bimpy.Image(np.uint8(np.kron([[1, 0] * 8, [0, 1] * 8] * 8, np.ones((20, 20))) * 255)) im.grayscale_to_alpha() if bimpy.button("Random Image"): imArr = np.random.rand(100,100,3) * 255 im = bimpy.Image(imArr.astype('uint8')) if bimpy.button("Print Image"): print(np.asarray(im)) if im is not None: bimpy.image(im) EOF
Bimpy requests and image processing cat <<-EOF | python import io import requests import bimpy from PIL import Image img_urls = [ "https://farm2.static.flickr.com/1080/1029412358_7ee17550fc.jpg", "https://farm5.static.flickr.com/4029/4332778219_472a339b0a.jpg", "https://bellard.org/bpg/3.png" ] def download_image(url): r = requests.get(url, timeout=4.0) if r.status_code != requests.codes.ok: assert False, 'Status code error: {}.'.format(r.status_code) data = io.BytesIO(r.content) return Image.open(data) ctx = bimpy.Context() ctx.init(800, 800, "Image") im = None while not ctx.should_close(): with ctx: bimpy.text("Example showing how to display images from PIL Image and numpy array") if bimpy.button("RGB image. Cat"): im = bimpy.Image(download_image(img_urls[0])) if bimpy.button("RGB image. Turtle"): im = bimpy.Image(download_image(img_urls[1])) if bimpy.button("RGB with alpha"): im = bimpy.Image(download_image(img_urls[2])) if bimpy.button("Generate mandelbrot set"): import numpy as np m = 480 n = 320 x = np.linspace(-2, 1, num=m).reshape((1, m)) y = np.linspace(-1, 1, num=n).reshape((n, 1)) C = np.tile(x, (n, 1)) + 1j * np.tile(y, (1, m)) Z = np.zeros((n, m), dtype=complex) M = np.full((n, m), True, dtype=bool) for i in range(100): Z[M] = Z[M] * Z[M] + C[M] M[np.abs(Z) > 2] = False im = bimpy.Image(np.uint8(np.flipud(1 - M) * 255)) if bimpy.button("Checkerboard"): import numpy as np im = bimpy.Image(np.uint8(np.kron([[1, 0] * 16, [0, 1] * 16] * 16, np.ones((20, 20))) * 255)) im.grayscale_to_alpha() y = bimpy.get_cursor_pos_y() bimpy.set_cursor_pos_y(250) window_pos = bimpy.get_window_pos() center = bimpy.Vec2(150, 350) + window_pos bimpy.text("Some text behind image. See 'Checkerboard' and 'RGB with alpha' options.") bimpy.add_circle_filled(center, 50, 0xaf4bb43c, 255) bimpy.set_cursor_pos_y(y) if im is not None: bimpy.image(im) EOF
C++ Pixel Drawing cat <<-EOF | g++ -I include -L lib -l SDL2 -x c++ - && ./a.out //How do you make a basic pixel drawing program? //https://gigi.nullneuron.net/gigilabs/sdl2-pixel-drawing/ //g++ pixelDrawing.cpp -I include -L lib -l SDL2 && ./a.out #include #include int main(int argc, char ** argv) { bool leftMouseButtonDown = false; bool quit = false; SDL_Event event; SDL_Init(SDL_INIT_VIDEO); SDL_Window * window = SDL_CreateWindow("SDL2 Pixel Drawing", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, 0); SDL_Renderer * renderer = SDL_CreateRenderer(window, -1, 0); SDL_Texture * texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STATIC, 640, 480); Uint32 * pixels = new Uint32[640 * 480]; memset(pixels, 255, 640 * 480 * sizeof(Uint32)); while (!quit) { SDL_UpdateTexture(texture, NULL, pixels, 640 * sizeof(Uint32)); SDL_WaitEvent(&event); switch (event.type) { case SDL_QUIT: quit = true; break; case SDL_MOUSEBUTTONUP: if (event.button.button == SDL_BUTTON_LEFT) leftMouseButtonDown = false; break; case SDL_MOUSEBUTTONDOWN: if (event.button.button == SDL_BUTTON_LEFT) leftMouseButtonDown = true; case SDL_MOUSEMOTION: if (leftMouseButtonDown) { int mouseX = event.motion.x; int mouseY = event.motion.y; pixels[mouseY * 640 + mouseX] = 0; } break; } SDL_RenderClear(renderer); SDL_RenderCopy(renderer, texture, NULL, NULL); SDL_RenderPresent(renderer); } delete[] pixels; SDL_DestroyTexture(texture); SDL_DestroyRenderer(renderer); SDL_DestroyWindow(window); SDL_Quit(); return 0; } EOF
Deno server cat <<-EOF | deno run -Ar - import { serve } from "https://deno.land/std@0.154.0/http/server.ts"; const port = 8080; const handler = (request: Request): Response => { const body = `Your user-agent is:\n\n${ request.headers.get("user-agent") ?? "Unknown" }`; return new Response(body, { status: 200 }); }; console.log(`HTTP webserver running. Access it at: http://localhost:8080/`); await serve(handler, { port }); EOF
Rust Fibonacci cat <<-EOF | rustc -o out - && ./out fn fib(n: i32) -> i32 { match n { 0 => 1, 1 => 1, n => fib(n-1) + fib(n-2), } } fn main(){ println!("{}", fib(8)); } EOF
Qt5 demo cat <<-EOF | python from PyQt5.QtWidgets import QApplication, QWidget app = QApplication([]) window = QWidget() window.show() app.exec() EOF
Use PySide6 virtual environment
# switch to PySide6 directory
cd /Users/davidsarmaholdings/Dropbox/DS/notebooks/PythonQT

# source activate testQt venv
(base) ds604:PythonQT davidsarmaholdings$ source testQt/bin/activate
PySide6 demo - use testQt env cat <<-EOF | python import sys from PySide6.QtWidgets import QApplication, QPushButton app = QApplication(sys.argv) window = QPushButton("Push Me") window.show() app.exec_() EOF
PySide6 Numpy array cat <<-EOF | python import sys from PySide6 import QtCore, QtGui, QtWidgets from PySide6.QtCore import Qt import numpy as np class TableModel(QtCore.QAbstractTableModel): def __init__(self, data): super(TableModel, self).__init__() self._data = data def data(self, index, role): if role == Qt.DisplayRole: # Note: self._data[index.row()][index.column()] will also work value = self._data[index.row(), index.column()] return str(value) def rowCount(self, index): return self._data.shape[0] def columnCount(self, index): return self._data.shape[1] class MainWindow(QtWidgets.QMainWindow): def __init__(self): super().__init__() self.table = QtWidgets.QTableView() data = np.array([ [1, 9, 2], [1, 0, -1], [3, 5, 2], [3, 3, 2], [5, 8, 9], ]) self.model = TableModel(data) self.table.setModel(self.model) self.setCentralWidget(self.table) app=QtWidgets.QApplication(sys.argv) window=MainWindow() window.show() app.exec_() EOF
PySide6 - Canvas Drawing cat <<-EOF | python import sys from PySide6 import QtCore, QtGui, QtWidgets from PySide6.QtCore import Qt class MainWindow(QtWidgets.QMainWindow): def __init__(self): super().__init__() self.label = QtWidgets.QLabel() canvas = QtGui.QPixmap(400, 300) canvas.fill(Qt.white) self.label.setPixmap(canvas) self.setCentralWidget(self.label) self.draw_something() def draw_something(self): canvas = self.label.pixmap() painter = QtGui.QPainter(canvas) painter.drawLine(10, 10, 300, 200) painter.end() self.label.setPixmap(canvas) app = QtWidgets.QApplication(sys.argv) window = MainWindow() window.show() app.exec_() EOF
PySide6 - Webkit browser cat <<-EOF | python from PySide6.QtCore import QUrl from PySide6.QtWidgets import QApplication from PySide6.QtWebEngineWidgets import QWebEngineView app = QApplication([]) view = QWebEngineView() view.load(QUrl("https://ds604.neocities.org/pythonGUI_01042023.html")) view.show() app.exec() EOF
PySide6 - Webkit, inline html
PySide6 - Webkit, run function from page
PySide6 - Webkit, run function from HTML template string page
PySide6 - Webkit, pass data between JS and Python functions
PySide6 - QML
PySide6 - Basic State Machine
Deno - npm import
Deno - chrome inspect
Deno - npm Express server
PySide 6 - load QML from URL
PySide 6 - QML hot reload
Deno server - display webpage
Deno server - router, get query params, eval command on server
Deno server - run code on server
Deno server - fixed initText issue, add route to get list of files
Deno server - eval on server, send result to browser
Deno - use Babel from npm
Deno - operator overloading using Babel
Build Qt terminal and gui apps from command line
cmake scripting
Parallel reduce - use map to reduce
Parallel reduce - using masks and generator function
Deno draw to canvas, save as image
Deno - open child process and communicate with it
Deno - run python command, take basic user input
Deno - nexpect, interactive node repl
Python - execute commands, command substitution, code repl
OpenAI - basic examples
Webcomponent - from html template string, or fetched html text
Flatten nested object
Swift GUI Appkit app
Deno Webview
Swift Basic Cocoa App
Swift Objective-C Cocoa App
Swift SwiftUI App without XCode
Swift Cocoa App - ViewController, app terminates when window closed
Swift setup to use Metal
Swift Core Graphics - draw a line
Swift - basic AppDelegate setup
SwiftUI view in Cocoa app, straightforward way
Deno File Watcher
Notes on parallel reduce
Parallel reduce - Masked sum propagation
Applescript and JXA examples
Swift JavaScriptCore example
JXA basic Cocoa Window example
Swift AST
Deno webview - run Deno function from window
Swift webview
Deno webview - eval on server
Nim GLFW window
Use Swift library from the repl
Deno netsaur - neural network
Deno python - matplotlib
Node webview - execute command in node
Node subprocess - execute Python program
Rust script example
Deno - run Python program in subprocess
Deno - run Python program from Express server
Deno webview - desktop
SwiftUI - display image from URL
Javascript data format - JSPath
Create Tauri app
Python Webview
C++ xtensor example
Nim Webgui
Elm basic example
Elm make - Deno watcher
Elm Parcel setup
Elm Repl - load file
Python repl OpenCV - load and show image
Roundtrip Pandas Dataframe to JS and back
OpenCV using CMake
OpenCV - process pixels, draw shapes
Octave - Matrix-vector product via textures
Octave - 2d convolution example
Numpy block matrices
Matrix-vector product - traditional vs GPU pipeline
Random sampling with a probability distribution
Nim - pixels library
C++ - lambda, vector, for_each
Nim - Processing basic example
C++ - std::vector map
Convolution via outer product