Start function on run with Flask












0















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










share|improve this question























  • 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
















0















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










share|improve this question























  • 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














0












0








0








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










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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



















  • 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












3 Answers
3






active

oldest

votes


















1














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()





share|improve this answer
























  • 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



















1














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.






share|improve this answer


























  • 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



















0














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.






share|improve this answer























    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    1














    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()





    share|improve this answer
























    • 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
















    1














    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()





    share|improve this answer
























    • 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














    1












    1








    1







    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()





    share|improve this answer













    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()






    share|improve this answer












    share|improve this answer



    share|improve this answer










    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



















    • 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













    1














    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.






    share|improve this answer


























    • 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
















    1














    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.






    share|improve this answer


























    • 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














    1












    1








    1







    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.






    share|improve this answer















    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.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    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



















    • 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











    0














    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.






    share|improve this answer




























      0














      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.






      share|improve this answer


























        0












        0








        0







        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.






        share|improve this answer













        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 23 '18 at 17:07









        Jim ToddJim Todd

        42737




        42737






























            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            Refactoring coordinates for Minecraft Pi buildings written in Python