Skip to content

TheRickMJ03/Atomica

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 

Repository files navigation

hackprinceton_BK

This guide explains how to set up and connect your Flask backend (running locally) with a Swift iOS app using the provided network connector. It covers installation, configuration, LAN access, endpoint testing, and troubleshooting.


📽️ Demo Video

Watch the demo


Project Overview

Your Flask app generates personalized 7-day plans using an AI backend (Dedalus MCP server) and serves them via REST endpoints. The Swift app collects user input (like wake-up time, focus habits, etc.) and requests the Flask backend to generate structured daily plans.

Flask Handles:

  • /api/create-personalized-plan → Generates a 7-day structured plan.
  • /api/get-tip → Returns short motivational tips.
  • /api/get-routine → Returns simple 7-day routines.

Swift Handles:

  • Collects user data (name, age, routines, etc.)
  • Sends POST requests to Flask via the NetworkService class.
  • Displays the returned JSON (plan, tips, intro) as part of the app UI.

Flask Backend Setup

1. Install Dependencies

pip install flask flask-cors dedalus_labs asyncio

2. Run the Flask App

Make sure app.py has this at the bottom:

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8080)

Then run:

python app.py

You should see something like:

 * Running on http://10.29.209.147:8080 (Press CTRL+C to quit)

Note that 10.29.209.147 is your local IP — this is what your friend's Swift app must use.

3. Firewall Configuration

Allow inbound connections on port 8080:

  • Windows: Allow the Python process or open TCP port 8080 on Private networks.
  • macOS: If prompted, click Allow incoming connections.

4. Test with Postman

Send a POST request to:

http://10.29.209.147:8080/api/create-personalized-plan

Body (JSON):

{
  "name": "Ricardo",
  "age": 22,
  "wakeUpTime": "8:00 am",
  "sleepTime": "7:00 pm",
  "eatingHabits": "I try, but I'm inconsistent",
  "exerciseRoutine": "I am very consistent",
  "deepFocus": "I am somewhat inconsistent",
  "timeManagement": "I am very consistent",
  "notificationStyle": "Firm and direct"
}

If you get a structured JSON response with intro, days, and tips, your Flask app is ready.


Swift iOS Integration

1. Set Base URL in NetworkService.swift

private let baseURL = "http://10.29.209.147:8080"

⚠️ Do not use https://127.0.0.1 — that points to your friend's local machine.

2. Endpoint Structure

Swift calls:

let url = URL(string: "\(baseURL)/api/create-personalized-plan")!

That sends the POST request to your running Flask app.

3. Request Body Mapping

Your Swift connector builds the JSON using:

struct FlaskUserData: Encodable {
    let name: String
    let age: Int
    let wakeUpTime: String
    let sleepTime: String
    let eatingHabits: String
    let exerciseRoutine: String
    let deepFocus: String
    let timeManagement: String
    let notificationStyle: String
}

This matches exactly what Flask expects — no changes needed.

4. Response Parsing

Your Flask backend returns:

{
  "intro": "...",
  "days": [
    {"day": 1, "morning": ["..."], "afternoon": ["..."], "evening": ["..."]},
    ...
  ],
  "tips": ["...", "..."]
}

Swift decodes it using:

struct FlaskPlanResponse: Codable {
    let intro: String
    let days: [FlaskDay]
    let tips: [String]
}

struct FlaskDay: Codable {
    let day: Int
    let morning: [String]
    let afternoon: [String]
    let evening: [String]
}

The models are fully compatible.


Local Network (LAN) Connection

To allow your friend's device to reach Flask:

  1. Both must be connected to the same Wi-Fi network.

  2. Use your local IP address (not 127.0.0.1).

  3. Ensure your firewall allows external access to port 8080.

  4. Test from your friend's browser:

    http://10.29.209.147:8080/
    

    It should display: API is running

If that works, the Swift app will also work.


Troubleshooting

Issue Cause Fix
Connection refused Firewall blocked port Allow Flask through firewall on 8080
Network unreachable Using 127.0.0.1 or wrong IP Replace with your LAN IP
Failed to decode response JSON format mismatch Ensure Swift request fields match Flask keys
⚠️ NSAppTransportSecurity error on iOS iOS blocks HTTP by default Add ATS exception in Xcode plist (see below)

iOS App Transport Security (ATS) Exception

In your app's Info.plist, add:

<key>NSAppTransportSecurity</key>
<dict>
  <key>NSAllowsArbitraryLoads</key>
  <true/>
</dict>

This allows HTTP connections for local development. You can remove it when deploying via HTTPS.


Future Upgrade: HTTPS or Remote Deployment

When you want to make it accessible outside your LAN, use one of these:

Option 1: ngrok (fastest)

ngrok http 8080

It gives you a temporary HTTPS URL like:

https://yourapp.ngrok-free.app

Update Swift's baseURL to this URL.

Option 2: Deploy to a Cloud Host

  • Render.com
  • Railway.app
  • Fly.io
  • Google Cloud Run / AWS / Azure

These platforms provide free HTTPS endpoints and persistent hosting.


Summary Checklist

Step Description Done
1️⃣ Run Flask with host='0.0.0.0'
2️⃣ Find your local IP via ipconfig / ifconfig
3️⃣ Use http://<your-ip>:8080 in Swift baseURL
4️⃣ Open port 8080 on your firewall
5️⃣ Test endpoint with Postman
6️⃣ Test Swift app call

Once all boxes are checked, your local Flask API and Swift connector will talk seamlessly over LAN.


🧩 Author Note: This setup is ideal for local development and iteration. When you're ready for production, switch from http:// to https:// using ngrok or a proper deployment host.


About

Atomica is an iOS app that generates personalized habit plans by analyzing your lifestyle patterns (wake time, energy levels, focus). It connects a Swift frontend to a Flask backend (Render-hosted) integrated with Gemini API and Dedalus MCP server for intelligent, adaptive recommendations.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages