How to create a Flask web app using PyCharm and PythonAnywhere

  1. Create a new Python project on PythonAnywhere.
  2. Copy the code below into a file named "app.py":  (Source)
    1. Flask API "Hello World" code
      #!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. Commands to start the local Flask web server (Windows)
          set FLASK_APP=app.py
          flask run 
      2. Mac:

        1. Commands to start the local Flask web server (Mac)
          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. Install the required Flask packages on PythonAnywhere that you saved with 'pip freeze'.
    1. pip install -r requirements.txt
  7. 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. 
  8. 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.
  9. Set the path to your virtualenv. 
    1. This is probably going to be the format: /home/YOUR_USERNAME/.virtualenvs/NAME_OF_THE_VIRTUAL_ENV
  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. Reload your web app. 
  12. You should now be able to navigate to the domain you used for the PythonAnywhere website and see the "Hello, World!" message.
  13. If you encounter any errors for the PythonAnywhere site, check the PythonAnywhere error log for the site.  A link to the error log is on the "Web" tab / page.
  14. And now, further developing the website is just a very slow process of thinking to yourself, "What small/incremental 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 and trying things out over the course of hours or days to figure out what commands you need to write to have that incremental 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?




Related websites