Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  1. Create a new Python project on PythonAnywhere.
  2. Copy the code below into a file named "app.py":  (Source)

    1. Code Block
      languagepy
      titleFlask API "Hello World" code
      collapsetrue
      #!flask/bin/python
      from flask import Flask
      
      app = Flask(__name__)
      
      
      @app.route('/')
      def index():
          return "Hello, World!"
      
      
      if __name__ == '__main__':
          app.run(debug=True)


  3. (Optional) Test the web app locally.
    1. Open a terminal and run the commands below:  (Source)
      1. Windows:


        1. Code Block
          languagebash
          titleCommands to start the local Flask web server (Windows)
          collapsetrue
          set FLASK_APP=app.py
          flask run 


      2. Mac:


        1. Code Block
          languagebash
          titleCommands to start the local Flask web server (Mac)
          collapsetrue
          export FLASK_APP=app.py
          flask run


    2. You should see a message saying something like "Running on http://127.0.0.1:5000/" 
    3. Navigate to that URL and you should see the text "Hello, World!". 
  4. Add flask and its dependencies to your requirements.txt.
    1. Open a terminal, navigate to the folder containing your app, and run "pip freeze > requirements.txt".
  5. Push the changes to your Bitbucket repository and then pull them down from PythonAnywhere.
  6. Create a new web app in PythonAnywhere. (Source)
    1. Click the 'Web' tab.
    2. Click 'Add a new web app'. 
    3. Choose the domain name to use. 
    4. Select a Python Web framework: Select "Manual configuration". 
    5. Select a Python version: Select the version that corresponds to the version you chose when creating your new project. 
  7. Set the path to your source code. 
    1. Note that if you have your server code in a subfolder of your Python project (like, "PROJECT_NAME/server/app.py"), it may be necessary to set the path here to the subfolder that contains the actual Python file.
  8. Set the path to your virtualenv. 
    1. This is probably going to be the format: /home/YOUR_USERNAME/.virtualenvs/NAME_OF_THE_VIRTUAL_ENV
  9. Reload your web app. 
  10. Set up your WSGI file.
    1. Open your WSGI config file. 
    2. Scroll down to the Flask section.
    3. Uncomment the lines of code. 
    4. Modify the path and module name to match your application. 
      1. Note that if you have your server code in a subfolder of your Python project (like, "PROJECT_NAME/server/app.py"), it may be necessary to set the path here to the subfolder that contains the actual Python file.
  11. You should now be able to navigate to path in app.py and see the "Hello, World!" message.
  12. And now, further developing the website is just a very slow process of thinking to yourself, "What small/incremental thing feature do I know that this website needs to have that doesn't depend on something else I haven't yet coded up?", and then Googling for up to a few hours and trying things out over the course of hours or days to figure out what commands you need to write to have that incremental thing feature added.
    1. Here is a very good article on how to quickly add registration to your app: https://medium.com/@perwagnernielsen/getting-started-with-flask-login-can-be-a-bit-daunting-in-this-tutorial-i-will-use-d68791e9b5b5
    2. Here's a guide I wrote on how to add Vue.js to your website: Stack Overflow - Nathan Wailes - How can I combine Vue.js with Flask?

...