Flask + Angular base project

Flask Angular base project

To begin with any Salesforce integration projects we need any third-party service that will use SF API. In my case I stop my choice on Flask(Python) for backend and Angular for frontend. As for me Flask is the best solution for small services as it is small, modular and fast enough. As an option can be also Pyramyd and Django, but first is quite tricky and second one too big for simple projects. With Flask I can build first prototype quickly. I don't want to say other solutions are bad - the right choice is up to you (maybe I will try to build same project with Django latter and we can compare both approaches).

Flask - https://flask.palletsprojects.com/en/1.1.x/
it can be found in the Pallets Project resource (with many other useful libs) - https://palletsprojects.com/

Angular - https://angular.io/

Let start with project structure. We need to setup Flask server and also setup Angular sub-project.

1. Create new folder for Project.

BACKEND:

2.1. Create subfolder ./backend

2.2. Create minimal Flask app in ./backend/app.py https://flask.palletsprojects.com/en/1.1.x/quickstart/#a-minimal-application
app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello, World!'
2.3. Add .env file to the root project folder with content:
FLASK_APP="./backend/app.py"
FLASK_ENV="development"


2.4. Create Python environment with Pipenv - https://github.com/pypa/pipenv
pipenv --python 3.8

2.5. Open virtual environment
pipenv shell

2.6. Install Flask
pipenv install Flask

2.7. Run backend sub-project with command
flask run

You should see this magic page in your browser (http://localhost:5000/)
Flask blank project started

FRONTEND:

3.1. Create Angular environment - https://angular.io/guide/setup-local
ng new frontend

3.2. Move package.json, package-lock.json and node_modules folder from ./frontend folder to root project folder (./). (! It is be important for latter Heroku instance setup).

3.3. Fix ./package.json to run angular project from subfolder
replace
"start": "ng serve",
by
"start": "cd ./frontend && ng serve",

3.4. Fix relative path to node_modules folder in ./frontend/angular.json.

3.5. Initialize git repository in your project folder and create .gitignore file with root node_modules as the first item.
git init

3.6. Test angular sub-project with npm command
npm start
and if all is fine you should see this pretty page in your browser (http://localhost:4200/)

Angular blank project started


So there is nothing special in this post, but it is great basement for backend/frontend complex project. In next post I will describe steps to compile frontend into static resource and make it available from python side.









Comments

Popular posts from this blog

HTTPS in local environment for Angular + Flask project.

Salesforce Authentication 2 (Token Validation and Refresh)

User authentification with Flask-Login