20231006-Fastapi-Streaming

  • main.py
from fastapi import FastAPI
from fastapi.responses import StreamingResponse
import time
app = FastAPI()
def fake_data_streamer():
	for i in range(10):
	yield b'some fake data\n\n'
	time.sleep(0.5)

@app.get('/')
async def main():
	return StreamingResponse(fake_data_streamer(), media_type='text/event-stream')
@app.get("/video")
def main():
def iterfile(): #
with open("rick_roll.mp4", mode="rb") as file_like: #
yield from file_like #
return StreamingResponse(iterfile(), media_type="video/mp4")
  • test.py
import httpx
url = 'http://127.0.0.1:8000/'
with httpx.stream('GET', url) as r:
for chunk in r.iter_raw(): # or, for line in r.iter_lines():
print(chunk)

Ref

20230929-Rn-Qrcode

def node_require(script)
# Resolve script with node to allow for hoisting
require Pod::Executable.execute_command('node', ['-p',
"require.resolve(
'#{script}',
{paths: [process.argv[1]]},
)", __dir__]).strip
end
node_require('react-native/scripts/react_native_pods.rb')
node_require('react-native-permissions/scripts/setup.rb')
...
platform :ios, min_ios_version_supported
prepare_react_native_project!
...
setup_permissions([
# 'AppTrackingTransparency',
# 'BluetoothPeripheral',
# 'Calendars',
'Camera',
# 'Contacts',
# 'FaceID',
# 'LocationAccuracy',
# 'LocationAlways',
# 'LocationWhenInUse',
# 'MediaLibrary',
# 'Microphone',
# 'Motion',
# 'Notifications',
'PhotoLibrary',
# 'PhotoLibraryAddOnly',
# 'Reminders',
# 'SpeechRecognition',
# 'StoreKit'
])
...
target 'verifier' do
	...
	pod 'RNPermissions', :path => '../node_modules/react-native-permissions'
  • ios/{}/Info.plist
<key>NSCameraUsageDescription</key>
<string>Your message to user when the camera is accessed for the first time</string>
<!-- Include this only if you are planning to use the camera roll -->
<key>NSPhotoLibraryUsageDescription</key>
<string>Your message to user when the photo library is accessed for the first time</string>
  • package.json 加上
...
"resolutions": {
"react-native-permissions": "^3.8.0"
},
"overrides": {
"react-native-qrcode-scanner": {
"react-native-permissions": "^3.8.0"
}
}

Ref

20230924-QR-Code

<QRCode

size={256}

style={{ height: "auto", maxWidth: "100%", width: "100%" }}

value={JSON.stringify(url)}

viewBox={`0 0 256 256`}

/>
  • @/components/Html5QrcodePlugin
import { Html5QrcodeScanner } from 'html5-qrcode';

import { useEffect } from 'react';

const qrcodeRegionId = "html5qr-code-full-region";

  

// Creates the configuration object for Html5QrcodeScanner.

const createConfig = (props) => {

let config = {};

if (props.fps) {

config.fps = props.fps;

}

if (props.qrbox) {

config.qrbox = props.qrbox;

}

if (props.aspectRatio) {

config.aspectRatio = props.aspectRatio;

}

if (props.disableFlip !== undefined) {

config.disableFlip = props.disableFlip;

}

return config;

};

  

const Html5QrcodePlugin = (props) => {

  

useEffect(() => {

// when component mounts

const config = createConfig(props);

const verbose = props.verbose === true;

// Suceess callback is required.

if (!(props.qrCodeSuccessCallback)) {

throw "qrCodeSuccessCallback is required callback.";

}

const html5QrcodeScanner = new Html5QrcodeScanner(qrcodeRegionId, config, verbose);

html5QrcodeScanner.render(props.qrCodeSuccessCallback, props.qrCodeErrorCallback);

  

// cleanup function when component will unmount

return () => {

html5QrcodeScanner.clear().catch(error => {

console.error("Failed to clear html5QrcodeScanner. ", error);

});

};

}, []);

  

return (

<div id={qrcodeRegionId} />

);

};

  

export default Html5QrcodePlugin;
import Html5QrcodePlugin from "@/components/Html5QrcodePlugin"
<Html5QrcodePlugin

fps={10}

qrbox={256}

disableFlip={false}

qrCodeSuccessCallback={(_results: string) => onScanSuccess(_results)}

/>

Ref

20230924-Wagami

import { EthereumClient, w3mConnectors, w3mProvider } from '@web3modal/ethereum' _app.tsx:

const chains = [sepolia,polygonMumbai, goerli,arbitrum, mainnet, polygon,localhost]

const projectId = ''

const { publicClient } = configureChains(chains, [w3mProvider({ projectId })])

const wagmiConfig = createConfig({

autoConnect: true,

connectors: w3mConnectors({ projectId, chains }),

publicClient

})
export default function App({ Component, pageProps }: AppProps) {

return (



<WagmiConfig config={wagmiConfig}>

<Component {...pageProps} />
import { usePrepareContractWrite, useContractWrite } from "wagmi";
const { data:max_tickets, isError, isLoading } = useContractRead({
address: router.query.contract as `0x${string}`,
abi: Event_data.abi,
functionName: 'max_tickets',
})
const { data: data1, write: set_tickets } = useContractWrite({ address: router.query.contract as `0x${string}`, abi: Event_data.abi, functionName: 'set_tickets' });
const { data, write, isSuccess } = useContractWrite({
address: contract_addr.event_factory as `0x${string}`,
abi: Event_data.abi,
functionName: 'createEvent',
})

const { isError, isLoading } = useWaitForTransaction({
hash: data?.hash,
})

Ref

0%