A very strange thing happened.
## app.py
from flask import Flask
from flask_cors import CORS
import numpy as np
import nibabel as nib
app = Flask(__name__)
CORS(app)
@app.route("/",methods=["POST"])
def readNifti():
img = nib.load("../data/test_project/sub-mrmdCrown_Hep3bLuc_11/ses-iv03/anat/sub-mrmdCrown_Hep3bLuc_11_ses-iv03_acq-TurboRARE_run-5_T2w.nii.gz")
data = img.get_fdata()
datamin=data.min()
datamax=data.max()
data = np.round((data-datamin)/(datamax-datamin)*200).astype(np.uint8).tobytes()
# return data
return np.ones((100,123),dtype=np.uint8).tobytes()
app.run(port=3413)
<template>
<div>
Hello World
</div>
</template>
<script setup lang="ts">
import { onMounted } from 'vue';
onMounted(() => {
fetch("http://localhost:3413", {
method: "POST"
})
.then(res => res.arrayBuffer())
.then((data) => {
console.log(data.byteLength);
})
})
</script>
Of course, this works fine. Vue application runs at port 3412. Flask application runs at port 3413.
However, if I change just one line of app.py
it becomes:
@app.route("/",methods=["POST"])
def readNifti():
img = nib.load("../data/test_project/sub-mrmdCrown_Hep3bLuc_11/ses-iv03/anat/sub-mrmdCrown_Hep3bLuc_11_ses-iv03_acq-TurboRARE_run-5_T2w.nii.gz")
data = img.get_fdata()
datamin=data.min()
datamax=data.max()
data = np.round((data-datamin)/(datamax-datamin)*200).astype(np.uint8).tobytes()
return data # this is the only line changed
return np.ones((100,123),dtype=np.uint8).tobytes()
Of course, it still works fine. Nevertheless, if I just change another line, it will fail.
@app.route("/",methods=["POST"])
def readNifti():
img = nib.load("../data/test_project/sub-mrmdCrown_Hep3bLuc_11/ses-iv03/anat/sub-mrmdCrown_Hep3bLuc_11_ses-iv03_acq-TurboRARE_run-5_T2w.nii.gz")
data = img.get_fdata()
datamin=data.min()
datamax=data.max()
# the next line: 200 -> 255, only this number is changed
data = np.round((data-datamin)/(datamax-datamin)*255).astype(np.uint8).tobytes()
return data
return np.ones((100,123),dtype=np.uint8).tobytes()
Vue code never changes. But browser says:
Access to fetch at 'http://localhost:3413/' from origin 'http://localhost:3412' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
How come the amplitude of the ndarray can affect CORS? Is it a bug? What can be the possible reason? And what can I do to solve this problem? I just wanted to shrink the size of image before transferring it.
As an aside, I use SSH local port forwarding to forward 3413 and 3412 from the Linux host to my windows machine.
By the way, flask restarts the app when it detects any change in the code. But it isn't always reliable. Please terminate the flask application and restart it every time you make any change to the python code.
Version information:
Chrome: Version 114.0.5735.199 (Official Build) (64-bit)
>>> import flask
>>> flask.__version__
'2.2.2'
>>> import flask_cors
>>> flask_cors.__version__
'3.0.10'
>>> import numpy
>>> numpy.__version__
'1.25.0'
>>> import nibabel
>>> nibabel.__version__
'5.1.0'
$ node -v
v16.13.1
$ npm -v
8.1.2
$ cat /etc/redhat-release
CentOS Linux release 7.9.2009 (Core)