Skip to content

Instantly share code, notes, and snippets.

View otmb's full-sized avatar

Manabu Otsu otmb

  • Infocom
  • Tokyo
View GitHub Profile
@otmb
otmb / progress_multipartparser_example.py
Created November 10, 2024 15:12
File upload progress with FastAPI
# https://www.starlette.io/requests/
# https://developer.mozilla.org/ja/docs/Web/API/Streams_API/Using_readable_streams
from fastapi import FastAPI, Request, HTTPException
from fastapi.responses import HTMLResponse, StreamingResponse, Response
from logging import getLogger, StreamHandler
import asyncio
import tempfile
import json
import time
import os
@otmb
otmb / README.md
Last active January 20, 2024 20:34
whisper.cpp Server Web. Interface
@otmb
otmb / CreatePDFView.swift
Last active September 21, 2023 23:07
Create multi-page PDFs with SwiftUI layouts
import SwiftUI
import PDFKit
let pdfRendererFormat = [
kCGPDFContextCreator: "@mbotsu",
kCGPDFContextAuthor: "@mbotsu",
kCGPDFContextTitle: "Create multi-page PDFs with SwiftUI layouts",
kCGPDFContextSubject: "Give up ImageRenderer and create PDFs with UIGraphicsPDFRenderer",
] as [String : Any]
@otmb
otmb / CreatePDFView.swift
Last active September 21, 2023 14:54
Create multi-page PDFs async using SwiftUI layouts
import SwiftUI
import PDFKit
let pdfRendererFormat = [
kCGPDFContextCreator: "@mbotsu",
kCGPDFContextAuthor: "@mbotsu",
kCGPDFContextTitle: "Create multi-page PDFs with SwiftUI layouts",
kCGPDFContextSubject: "Give up ImageRenderer and create PDFs with UIGraphicsPDFRenderer",
] as [String : Any]
@otmb
otmb / CreateImageView.swift
Created September 15, 2023 13:37
Create Image of SwiftUI layouts
import SwiftUI
import Charts
struct CreateImageView: View {
@State var uiImage: UIImage?
var body: some View {
VStack {
if let uiImage = uiImage {
Image(uiImage: uiImage)
}
@otmb
otmb / CreatePDFView.swift
Last active September 12, 2023 17:40
Create multi-page PDFs with SwiftUI layouts
import SwiftUI
import PDFKit
let pdfRendererFormat = [
kCGPDFContextCreator: "@mbotsu",
kCGPDFContextAuthor: "@mbotsu",
kCGPDFContextTitle: "Create multi-page PDFs with SwiftUI layouts",
kCGPDFContextSubject: "Give up ImageRenderer and create PDFs with UIGraphicsPDFRenderer",
] as [String : Any]
@otmb
otmb / PDFCombineView.swift
Last active September 10, 2023 08:58
Create and combine PDFs of SwiftUI layouts
import SwiftUI
import PDFKit
let pdfDocumentOptions = [
kCGPDFContextCreator: "@mbotsu",
kCGPDFContextAuthor: "@mbotsu",
kCGPDFContextTitle: "Create and combine PDFs with SwiftUI layouts",
kCGPDFContextSubject: "Give up ImageRenderer and create PDFs with UIGraphicsPDFRenderer",
] as [PDFDocumentWriteOption : Any]
@otmb
otmb / ContentView.swift
Last active September 9, 2023 12:53
When outputting PDF with SwiftUI, Table cannot be used with ImageRenderer, so use UIGraphicsPDFRenderer
import SwiftUI
struct ContentView: View {
@State var pdfUrl: URL?
var body: some View {
VStack {
if let pdfUrl = pdfUrl {
PreviewView(url: pdfUrl)
}
}
@otmb
otmb / iosConversions.swift
Last active February 19, 2023 10:18
When you can't set `-all_load` to Other Linker Flags
// https://github.com/opencv/opencv/blob/master/modules/imgcodecs/src/ios_conversions.mm
// https://github.com/opencv/opencv/blob/master/modules/imgcodecs/src/apple_conversions.mm
func MatToCGImage(image:Mat) -> CGImage? {
let data = Data(bytes: image.dataPointer(), count: image.step1() * Int(image.rows()))
let colorSpace = image.elemSize() == 1 ? CGColorSpaceCreateDeviceGray() : CGColorSpaceCreateDeviceRGB()
let provider = CGDataProvider(data: data as CFData);
let alpha = image.channels() == 4;
let bitmapInfo: CGBitmapInfo = CGBitmapInfo(rawValue: (alpha ? CGImageAlphaInfo.last : CGImageAlphaInfo.none).rawValue | CGBitmapInfo.byteOrderDefault.rawValue)
@otmb
otmb / ContentView.swift
Last active August 1, 2024 01:18
WKWebView can read a wasm from a localfile with XMLHttpRequest, but returns an error with fetch
import SwiftUI
import WebKit
enum WebViewError: Error {
case contentConversion(String)
case emptyFileName
case inivalidFilePath
var message: String {
switch self {