Start function on run with Flask
I have a flask app
@app.route("/hello")
def generater():
return "hello world
if __name__ == '__main__':
app.run()
My application runs fine, but i would like to know how I could make a request to http://127.0.0.1:5000/hello when I compile my code
python django flask frameworks
add a comment |
I have a flask app
@app.route("/hello")
def generater():
return "hello world
if __name__ == '__main__':
app.run()
My application runs fine, but i would like to know how I could make a request to http://127.0.0.1:5000/hello when I compile my code
python django flask frameworks
What do you mean by "compile"?
– Edgar Ramírez Mondragón
Nov 23 '18 at 17:35
When i execute the command python hello.py the first thing that would happen is that it would go to localhost:port/hello and it would return hello world without me having to go to a webpage and type localhost/hello
– rabiaasif
Nov 23 '18 at 18:09
add a comment |
I have a flask app
@app.route("/hello")
def generater():
return "hello world
if __name__ == '__main__':
app.run()
My application runs fine, but i would like to know how I could make a request to http://127.0.0.1:5000/hello when I compile my code
python django flask frameworks
I have a flask app
@app.route("/hello")
def generater():
return "hello world
if __name__ == '__main__':
app.run()
My application runs fine, but i would like to know how I could make a request to http://127.0.0.1:5000/hello when I compile my code
python django flask frameworks
python django flask frameworks
asked Nov 23 '18 at 17:01
rabiaasifrabiaasif
328
328
What do you mean by "compile"?
– Edgar Ramírez Mondragón
Nov 23 '18 at 17:35
When i execute the command python hello.py the first thing that would happen is that it would go to localhost:port/hello and it would return hello world without me having to go to a webpage and type localhost/hello
– rabiaasif
Nov 23 '18 at 18:09
add a comment |
What do you mean by "compile"?
– Edgar Ramírez Mondragón
Nov 23 '18 at 17:35
When i execute the command python hello.py the first thing that would happen is that it would go to localhost:port/hello and it would return hello world without me having to go to a webpage and type localhost/hello
– rabiaasif
Nov 23 '18 at 18:09
What do you mean by "compile"?
– Edgar Ramírez Mondragón
Nov 23 '18 at 17:35
What do you mean by "compile"?
– Edgar Ramírez Mondragón
Nov 23 '18 at 17:35
When i execute the command python hello.py the first thing that would happen is that it would go to localhost:port/hello and it would return hello world without me having to go to a webpage and type localhost/hello
– rabiaasif
Nov 23 '18 at 18:09
When i execute the command python hello.py the first thing that would happen is that it would go to localhost:port/hello and it would return hello world without me having to go to a webpage and type localhost/hello
– rabiaasif
Nov 23 '18 at 18:09
add a comment |
3 Answers
3
active
oldest
votes
You can use webbrowser to automatically open http://localhost:5000
in a web browser when running your flask app:
import webbrowser
...
if __name__ == '__main__':
webbrowser.open('http://localhost:5000')
app.run()
this is exactly what I was looking for just need to try it on the server now! thank you
– rabiaasif
Nov 26 '18 at 22:29
add a comment |
There are a lot of ways you could do this. You could just open up your browser to that location. You could try @jimtodd's answer and then cURL the endpoint from another terminal window.
To do it in the code, which I guess is what you want, Flask offers you some helper methods. For example there is: http://flask.pocoo.org/docs/1.0/api/#flask.Flask.before_first_request
You can use it like this:
def foo():
pass
app.before_first_request(foo)
In the case where you want to run a script strictly on run, not just before the first request, this solution is good: Run code after flask application has started -- I guess you would use this for cold-start problems as well.
I dont want this to happen before I make a request, when I run python hello.py i want it to run foo without having to open a browser and make a request
– rabiaasif
Nov 23 '18 at 18:14
Edited my response to address this
– Charles Landau
Nov 23 '18 at 18:20
add a comment |
You can do this from command prompt:
set FLASK_APP=hello.py
python -m flask run
The you will see....
Running on http://127.0.0.1:5000
Now you can check the output in your browser.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53450540%2fstart-function-on-run-with-flask%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use webbrowser to automatically open http://localhost:5000
in a web browser when running your flask app:
import webbrowser
...
if __name__ == '__main__':
webbrowser.open('http://localhost:5000')
app.run()
this is exactly what I was looking for just need to try it on the server now! thank you
– rabiaasif
Nov 26 '18 at 22:29
add a comment |
You can use webbrowser to automatically open http://localhost:5000
in a web browser when running your flask app:
import webbrowser
...
if __name__ == '__main__':
webbrowser.open('http://localhost:5000')
app.run()
this is exactly what I was looking for just need to try it on the server now! thank you
– rabiaasif
Nov 26 '18 at 22:29
add a comment |
You can use webbrowser to automatically open http://localhost:5000
in a web browser when running your flask app:
import webbrowser
...
if __name__ == '__main__':
webbrowser.open('http://localhost:5000')
app.run()
You can use webbrowser to automatically open http://localhost:5000
in a web browser when running your flask app:
import webbrowser
...
if __name__ == '__main__':
webbrowser.open('http://localhost:5000')
app.run()
answered Nov 23 '18 at 19:06
Edgar Ramírez MondragónEdgar Ramírez Mondragón
1,5512821
1,5512821
this is exactly what I was looking for just need to try it on the server now! thank you
– rabiaasif
Nov 26 '18 at 22:29
add a comment |
this is exactly what I was looking for just need to try it on the server now! thank you
– rabiaasif
Nov 26 '18 at 22:29
this is exactly what I was looking for just need to try it on the server now! thank you
– rabiaasif
Nov 26 '18 at 22:29
this is exactly what I was looking for just need to try it on the server now! thank you
– rabiaasif
Nov 26 '18 at 22:29
add a comment |
There are a lot of ways you could do this. You could just open up your browser to that location. You could try @jimtodd's answer and then cURL the endpoint from another terminal window.
To do it in the code, which I guess is what you want, Flask offers you some helper methods. For example there is: http://flask.pocoo.org/docs/1.0/api/#flask.Flask.before_first_request
You can use it like this:
def foo():
pass
app.before_first_request(foo)
In the case where you want to run a script strictly on run, not just before the first request, this solution is good: Run code after flask application has started -- I guess you would use this for cold-start problems as well.
I dont want this to happen before I make a request, when I run python hello.py i want it to run foo without having to open a browser and make a request
– rabiaasif
Nov 23 '18 at 18:14
Edited my response to address this
– Charles Landau
Nov 23 '18 at 18:20
add a comment |
There are a lot of ways you could do this. You could just open up your browser to that location. You could try @jimtodd's answer and then cURL the endpoint from another terminal window.
To do it in the code, which I guess is what you want, Flask offers you some helper methods. For example there is: http://flask.pocoo.org/docs/1.0/api/#flask.Flask.before_first_request
You can use it like this:
def foo():
pass
app.before_first_request(foo)
In the case where you want to run a script strictly on run, not just before the first request, this solution is good: Run code after flask application has started -- I guess you would use this for cold-start problems as well.
I dont want this to happen before I make a request, when I run python hello.py i want it to run foo without having to open a browser and make a request
– rabiaasif
Nov 23 '18 at 18:14
Edited my response to address this
– Charles Landau
Nov 23 '18 at 18:20
add a comment |
There are a lot of ways you could do this. You could just open up your browser to that location. You could try @jimtodd's answer and then cURL the endpoint from another terminal window.
To do it in the code, which I guess is what you want, Flask offers you some helper methods. For example there is: http://flask.pocoo.org/docs/1.0/api/#flask.Flask.before_first_request
You can use it like this:
def foo():
pass
app.before_first_request(foo)
In the case where you want to run a script strictly on run, not just before the first request, this solution is good: Run code after flask application has started -- I guess you would use this for cold-start problems as well.
There are a lot of ways you could do this. You could just open up your browser to that location. You could try @jimtodd's answer and then cURL the endpoint from another terminal window.
To do it in the code, which I guess is what you want, Flask offers you some helper methods. For example there is: http://flask.pocoo.org/docs/1.0/api/#flask.Flask.before_first_request
You can use it like this:
def foo():
pass
app.before_first_request(foo)
In the case where you want to run a script strictly on run, not just before the first request, this solution is good: Run code after flask application has started -- I guess you would use this for cold-start problems as well.
edited Nov 23 '18 at 18:21
answered Nov 23 '18 at 17:13
Charles LandauCharles Landau
2,4231216
2,4231216
I dont want this to happen before I make a request, when I run python hello.py i want it to run foo without having to open a browser and make a request
– rabiaasif
Nov 23 '18 at 18:14
Edited my response to address this
– Charles Landau
Nov 23 '18 at 18:20
add a comment |
I dont want this to happen before I make a request, when I run python hello.py i want it to run foo without having to open a browser and make a request
– rabiaasif
Nov 23 '18 at 18:14
Edited my response to address this
– Charles Landau
Nov 23 '18 at 18:20
I dont want this to happen before I make a request, when I run python hello.py i want it to run foo without having to open a browser and make a request
– rabiaasif
Nov 23 '18 at 18:14
I dont want this to happen before I make a request, when I run python hello.py i want it to run foo without having to open a browser and make a request
– rabiaasif
Nov 23 '18 at 18:14
Edited my response to address this
– Charles Landau
Nov 23 '18 at 18:20
Edited my response to address this
– Charles Landau
Nov 23 '18 at 18:20
add a comment |
You can do this from command prompt:
set FLASK_APP=hello.py
python -m flask run
The you will see....
Running on http://127.0.0.1:5000
Now you can check the output in your browser.
add a comment |
You can do this from command prompt:
set FLASK_APP=hello.py
python -m flask run
The you will see....
Running on http://127.0.0.1:5000
Now you can check the output in your browser.
add a comment |
You can do this from command prompt:
set FLASK_APP=hello.py
python -m flask run
The you will see....
Running on http://127.0.0.1:5000
Now you can check the output in your browser.
You can do this from command prompt:
set FLASK_APP=hello.py
python -m flask run
The you will see....
Running on http://127.0.0.1:5000
Now you can check the output in your browser.
answered Nov 23 '18 at 17:07
Jim ToddJim Todd
42737
42737
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53450540%2fstart-function-on-run-with-flask%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What do you mean by "compile"?
– Edgar Ramírez Mondragón
Nov 23 '18 at 17:35
When i execute the command python hello.py the first thing that would happen is that it would go to localhost:port/hello and it would return hello world without me having to go to a webpage and type localhost/hello
– rabiaasif
Nov 23 '18 at 18:09