'how to define origin_check in pywebio start_server
I have a small test script where I'd like to check the pywebio start_server arguments. But neither allowed_origins
nor check_origin
seems to work in my script:
from pywebio import start_server
from pywebio.input import *
from pywebio.output import *
def main():
put_markdown("## just a small start_server test")
def my_origin_check(value):
print(value)
return False
if __name__ == '__main__':
start_server(main, check_origin=my_origin_check, debug=True, port=5000)
I can't see the reason why my_origin_check
doesn't get called - I'm blind on my eyes ... any help?
Solution 1:[1]
According to the source code of start_server()
:
check_origin_func = lambda origin, handler: _is_same_site(origin, handler) or check_origin(origin)
the user-defined check_origin
function only is called when the application url and the underlying websocket api is not on same host
Solution 2:[2]
As it's only doing optimisation over one variable, optimize
should work fine e.g.
fnToFindRoot = function(x, a=0.07) {
y <- data[,(mean(pnorm(qnorm(var1)+x)) - a)^2]
print(sprintf("x: %s, y:%s", x, y))
y
}
rootSearch = optimize(fnToFindRoot, interval=c(-5, 5), a=0.07)
fnToFindRoot(rootSearch$minimum)
The problem with the way you had it setup is that the optim function is always trying to minimise the objective. The way you were writing it, it was trying to minimise mean(pnorm(qnorm(var1)+x))
with 0.07
as the starting value of x
. Instead, you want to get the objective as close to 0.07 as possible, so minimise (mean(pnorm(qnorm(var1)+x)) - a)^2
.
The interval
controls the range of x that optimize can use
edit: I was using made up data, so check if rootSearch$minimum
works for you. My made up data:
set.seed(1)
data <- data.table()
data[, var1 := runif(100, 0.04, 0.45)]
> fnToFindRoot(rootSearch$minimum)
[1] "x: -0.857685927870974, y:4.1043516952502e-13"
[1] 4.104352e-13
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Jonny Phelps |