Flask in PyCharm Community Edition
--
Run and Config a Flask App in PyCharm Community Edition
As we know there is no direct support to start a flask project in in PyCharm Community Edition unlike the Professional Version. So we have to setup everything from scratch. So here are few simple steps to start a flask project and set it up.
PreRequisite : You have pip and python3 installed. If you don’t then run these commands in terminal
brew install pip
pip -versionbrew install python
- Start a Pure python project and give the project name (flaskTest) and select the virtualenv
- Create two files (requirement.txt) which holds all python dependencies and a simple python file (test.py)
Add these dependencies in the requirement.txt file
# This file is used by pip to install required python packages
# Usage: pip install -r requirements.txt
# Flask Framework
click==7.1.2
Flask==2.0.2
itsdangerous==2.0.1
Jinja2==3.0.0
MarkupSafe==2.0.0rc2
Werkzeug==2.0.0
# Flask Packages
Flask-Login==0.4.0
Flask-Migrate==2.0.2
Flask-Script==2.0.5
Flask-SQLAlchemy==2.4.0
Flask-WTF==0.14.2
Flask-User==1.0.1.5
# Automated tests
pytest==3.0.5
pytest-cov==2.4.0
There will be install plugin message on top of the requirements.txt file. Click it to download the plugins. If not then open terminal from the bottom and run this command
pip install -r requirements.txt
And add these line in test.py
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'hello test'
if __name__ == '__main__':
app.run()
Now lets setup the configuration
- Select Add configuration on top right of the PyCharm CE and Add a Python configuration and name the configuration (Test)
2. Set up Script Path: We need to setup flask plugin path here as below:
3. Set Environment Variable.
1. FLASK_APP : Test.py
2. FLASK_ENV : development
3. FLASK_DEBUG : 1
FLASK_DEBUG helps you for hot reloading. When you change your code and save. The flask server will restart with new modified code.
Now run the flask app by clicking the run button beside the configuration. The flask server will be launched and the app hosted at http://localhost:5000/
You can open postman and hit the url to get the response or can open that in a web browser.
PS : If the flask app don’t run then to be on safer side run this command to set the flask app env variable in venv terminal
export FLASK_APP=test.pyflask run
Next Part we will see how to Create few simple API’s in Flask and host it locally and set up a DB to store the data and carry out all CRUD operations using APIs and unit test it.