Welcome to our project! This is a simple yet powerful full-stack application showcasing the synergy between Flask serving as the backend and Ionic Angular lighting up the frontend. Perfect for both learning and building, this setup allows you to serve data dynamically and present it beautifully.
Follow these instructions to get a copy of the project up and running on your local machine for development and testing purposes. Let's dive in!
Before we start, make sure you have these essentials:
- Python (with pip)
- Node.js and npm
- Ionic CLI
Let's set up our development environment step-by-step to breathe life into our project.
-
Install Flask and Flask-CORS
Open your terminal and run:
pip install Flask flask-cors
-
Create the Flask Application
Create a file named
app.py
and inject the following Python code to kickstart a basic Flask app with CORS enabled:from flask import Flask from flask_cors import CORS app = Flask(__name__) CORS(app) @app.route('/getid') def getid(): return '''[ { "id": 1, "title": "My first blog post" }, { "id": 2, "title": "My second blog post" } ]''' app.run()
-
Run the Flask App
Fire up your Flask application:
python app.py
-
Create an Ionic Angular App
Generate a fresh Ionic Angular application using the Ionic CLI:
ionic start example1 blank --type=angular
-
Add a Service for REST API Calls
Inside your project directory, spawn a service:
cd example1 ionic generate service ionic-rest
-
Configure AppModule
Amend
app.module.ts
to embraceHttpClientModule
and your novel service:// Other imports... import { HttpClientModule } from '@angular/common/http'; import { IonicRestService } from './ionic-rest.service'; @NgModule({ // Other module properties... imports: [ // Other modules... HttpClientModule, ], providers: [ IonicRestService, // Other providers... ], }) export class AppModule {}
-
Implement Data Fetching in the Service
Adjust
ionic-rest.service.ts
for fetching data from our Flask API:// Other imports... import { HttpClient } from '@angular/common/http'; import { lastValueFrom, take } from 'rxjs'; @Injectable({ providedIn: 'root', }) export class IonicRestService { URL: string = 'http://127.0.0.1:5000/getid'; constructor(private httpClient: HttpClient) {} async fetchPosts(): Promise<any> { let request = this.httpClient.get(this.URL).pipe(take(1)); return await lastValueFrom<any>(request); } }
-
Display Data in HomePage
Update
home.page.ts
andhome.page.html
to fetch and exhibit data upon a button press.
Kickstart your Ionic application:
ionic serve
Your Flask backend should now be serving data at http://127.0.0.1:5000/getid
, and your Ionic Angular application should fetch this data to display it gracefully.
- Flask - The web framework used for the backend.
- Ionic - The framework used for the frontend.
- Angular - Empowers building the frontend.
- Your Name - Feel free to add your contact information and social media links!
This project is licensed under the MIT License - see the LICENSE.md file for details.
- Hat tip to anyone whose code was used
- Inspiration
- ME
- My brain
Thank you for stopping by, and happy coding! 😊👨💻