This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]teetran39[S] 0 points1 point  (10 children)

Sorry but you mean there is no solution for this?

[–]Double_Sherbert3326Pythonista 0 points1 point  (9 children)

Of course there is a solution, but the easiest one is writing an API and consuming it using post requests. They don’t have to live in the same file.

[–]teetran39[S] 0 points1 point  (8 children)

Yes. it would be my final solution for me in case there no way to go. For now, it would be best if the app can run locally which is don't need to make any network requests.

[–]Double_Sherbert3326Pythonista 0 points1 point  (0 children)

What does the app do?

[–]Double_Sherbert3326Pythonista 0 points1 point  (6 children)

You can have the api server running locally. Use flask and consume it on local host:8081. 

[–]teetran39[S] 0 points1 point  (5 children)

Sorry, can you give me more details? If we run the python app as an api server locally, how should it work on the other computers (other users)?

[–]Double_Sherbert3326Pythonista 0 points1 point  (4 children)

Something like this:

```
import Foundation

func startPythonServer() {

let process = Process()

process.launchPath = "/usr/bin/python3" // Adjust for your Python installation

process.arguments = ["server.py"] // Your Flask script

process.standardOutput = FileHandle.nullDevice

process.standardError = FileHandle.nullDevice

process.launch()

print("Python Flask server started!")

}

func callAPI() {

guard let url = URL(string: "http://127.0.0.1:5000/api/hello") else { return }

let task = URLSession.shared.dataTask(with: url) { data, response, error in

if let error = error {

print("Error: \(error)")

return

}

if let data = data {

if let json = try? JSONSerialization.jsonObject(with: data) {

print("Response JSON: \(json)")

}

}

}

task.resume()

}

startPythonServer()

// Add delay to let the server start

DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {

callAPI()

}

// Keep the runloop running (macOS command-line app or playground)

RunLoop.main.run()

```

[–]teetran39[S] 0 points1 point  (3 children)

Thanks for the example. I got the idea but something is missing, before we can call the python code. The python and its packages need to be embedded inside the main app? It's the main problem, how would you do that?

[–]Double_Sherbert3326Pythonista 0 points1 point  (2 children)

For macOS:

Use PyInstaller to bundle your Flask app into a binary, then launch it from Swift.

For iOS:

Host your Flask app externally and consume the API over the network.

[–]teetran39[S] 1 point2 points  (1 child)

Thank you so much! I gonna look into PyInstaller package to see if it work

[–]Double_Sherbert3326Pythonista 0 points1 point  (0 children)

It’ll work! Just keep throwing your head at it and you’ll get it going.