4

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)
jared
  • 4,165
  • 1
  • 8
  • 31
Septem
  • 41
  • 3
  • I'm not familiar with flask, but all the story of CORS is about specific headers, which add in specific middlewares in backend. probably changing in flow of your data affects on headers and adding them, but this is not related to vue. – Ali Bahrami Jul 23 '23 at 11:39
  • 2
    Maybe the browser is inferring content type from the bytes being sent? Does manually setting a [content type](https://stackoverflow.com/questions/11773348/python-flask-how-to-set-content-type) result in more consistent behavior? – Nick ODell Jul 23 '23 at 15:36
  • Any way you could add a https://stackoverflow.com/help/minimal-reproducible-example? – jub0bs Jul 23 '23 at 20:06

1 Answers1

0

I've found the reason. I happened to see the log of the antivirus software (Sophos) and found that http://localhost was blocked. That's probably why.

Sorry I didn't provide a minimal reproducible example. I gave up writing the application in the form of frontend and backend and decided to start all over again and make it a desktop software.

Thanks everybody for your kindness!

Septem
  • 41
  • 3