...
- Create a new Python project on PythonAnywhere.
- Copy the code below into a file named "app.py": (Source)
Code Block language py title Flask API "Hello World" code collapse true #!flask/bin/python from flask import Flask app = Flask(__name__) @app.route('/') def index(): return "Hello, World!" if __name__ == '__main__': app.run(debug=True)
- (Optional) Test the web app locally.
- Open a terminal and run the commands below: (Source)
Windows:
Code Block language bash title Commands to start the local Flask web server (Windows) collapse true set FLASK_APP=app.py flask run
Mac:
Code Block language bash title Commands to start the local Flask web server (Mac) collapse true export FLASK_APP=app.py flask run
- You should see a message saying something like "Running on http://127.0.0.1:5000/"
- Navigate to that URL and you should see the text "Hello, World!".
- Open a terminal and run the commands below: (Source)
- Add flask and its dependencies to your requirements.txt.
- Open a terminal, navigate to the folder containing your app, and run "pip freeze > requirements.txt".
- Push the changes to your Bitbucket repository and then pull them down from PythonAnywhere.
- Create a new web app in PythonAnywhere. (Source)
- Click the 'Web' tab.
- Click 'Add a new web app'.
- Choose the domain name to use.
- Select a Python Web framework: Select "Manual configuration".
- Select a Python version: Select the version that corresponds to the version you chose when creating your new project.
- Set the path to your source code.
- 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.
- Set the path to your virtualenv.
- This is probably going to be the format: /home/YOUR_USERNAME/.virtualenvs/NAME_OF_THE_VIRTUAL_ENV
- Reload your web app.
- Set up your WSGI file.
- Open your WSGI config file.
- Scroll down to the Flask section.
- Uncomment the lines of code.
- Modify the path and module name to match your application.
- 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.
- You should now be able to navigate to path in app.py and see the "Hello, World!" message.
- 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.
- 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
- 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?
...