Nathan Wailes - Blog - GitHub - LinkedIn - Patreon - Reddit - Stack Overflow - Twitter - YouTube
How to create a Flask web app using PyCharm and PythonAnywhere
- Create a new Python project on PythonAnywhere.
- Copy the code below into a file named "app.py": (Source)
- (Optional) Test the web app locally.
- Open a terminal and run the commands below: (Source)
Windows:
Mac:
- 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.
- Install the required Flask packages on PythonAnywhere that you saved with 'pip freeze'.
pip install -r requirements.txt
- 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
- 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.
- Reload your web app.
- You should now be able to navigate to the domain you used for the PythonAnywhere website and see the "Hello, World!" message.
- 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.
- 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.
- 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?
Related websites
- O'Reilly Media - Building Web APIs with Flask (video tutorials) - by Miguel Grinberg
- GitHub - Miguel Grinberg - oreilly-flask-apis-video - This is the repo for the code written in the video tutorials.
- MiguelGrinberg.com - Designing a RESTful API with Python and Flask
- O'Reilly Media - Flask Web Development (book) - There's a chapter on APIs.
- O'Reilly Media - Web API Development with Flask (Video tutorials) - These are not the ones by Grinberg.
- Official Flask website - Quickstart
- Only the beginning is relevant (the 'hello world' example), the rest of the page is discussing features used when building a website.
- 2014.05.12 - O'Reilly Media - Building RESTful APIs with Flask in PyCharm - blog post