0

I have a problem with my localhost Flask server app.py, where i have my data and logs functions. My full project goes on PC and on Android, only my server localhost does not work on Android. When I run the project app on Chrome on windows, the Project runs, but when I run the project on Android Drive, then it does not read my server. My ionic app runs on ionic serve (path:8100).

My localhost, where my data is, runs on localhost:5000 serve.

My Flask server Python App.py

from flask import Flask, jsonify,request
from flask_cors import CORS, cross_origin
from posts import posts
from waitress import serve


app=Flask(__name__)
cors = CORS(app)
app.config['CORS_HEADERS'] = 'Content-Type'

@app.route('/')
@cross_origin()
def index():
    lang = request.headers.get("Accept-Language", 'sk')[:2]

    p = list(map (lambda post: translate(post, lang), posts))
    
    return jsonify(p)


def translate(post, lang):
    translation = next (t for t in post['translations'] if t['locale'] == lang)

    return {
        "id": post ['id'],
        "title": translation ['title'],
        "description": translation ['description'],
        "image": post['image'],
}
if __name__ == "__main__":
  from waitress import serve
  serve(app, host="localhost", port=5000
  )

My ionic project page "Novinky"

import { Component, OnInit } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';


@Component({
  selector: 'app-posts2',
  templateUrl: './posts.component.html',
  styleUrls: ['./posts.component.scss'],
})
export class PostsComponent implements OnInit {
  
  posts:any = [];
  constructor(private http: HttpClient) { }

  ngOnInit() {
    const lang = localStorage.getItem('lang') || 'sk';
    
    const headers = new HttpHeaders({
      'Accept-Language': lang
    })   
  
  
    this.http.get('http://localhost:5000', {
      headers: headers
    }).subscribe(data=>{
      console.log();
      this.posts = data;
       });
  }

}

When I run my project on PC i see this

enter image description here

This is my project screen. enter image description here

When I run my project on Android Studio not work i see black screen, not reed the localhost server!

enter image description here

Please help mee, thanks.

hkBst
  • 2,818
  • 10
  • 29
Din14
  • 41
  • 4
  • Thanks for fast answer. And I need install this plugin npx localtunnel --port 5000 – Din14 Feb 25 '22 at 10:38
  • and how i applie this on my script? – Din14 Feb 25 '22 at 10:39
  • See if [this](https://stackoverflow.com/questions/62098072/how-to-access-flask-web-server-from-mobile-device) helps. Just need to put your ipv4 address instead of localhost(Assuming your machine and android device are on same LAN) – k33da_the_bug Feb 25 '22 at 10:57
  • thanks,yes this go, super. Please and when the app go at google pay and app pay, then will run me external dat with this method? I need to record new data to app . – Din14 Feb 25 '22 at 11:09
  • For simplicity, when you officially deploying your app on play/app store then you have to host flask app on cloud (You can google this at it involves so many steps). Once your flask server is hosted you will get your server ip/url for accessing it over the internet. You then just need to replace your current local ip with your hosted server ip/url. Assume currently you have locally hosted flask app with `192.168.x.x` and once you host on cloud , you will get unique url `www.yourbusiness.com` then you have to replace `192.168.x.x` with `www.yourbusiness.com` in ionic app as it is going live. – k33da_the_bug Feb 25 '22 at 11:19
  • Thank you, yes exp. firebase : firestore? free cloud, without pricing not exist,for example? – Din14 Feb 25 '22 at 11:40
  • My Flask app go on http://127.0.0.1:5000 is this real? – Din14 Feb 25 '22 at 11:41
  • When you are going live you have to deploy your flask app. Example hosting with some free tier access would be [pythonanywher.com](https://www.pythonanywhere.com/) or you can use aws,gcp depending on your needs. – k33da_the_bug Feb 25 '22 at 11:50

3 Answers3

0

Assuming your flask and android app is on same LAN,

in flask, just update your host to your local ipv4 address

serve(app, host="192.168.X.X", port=5000)

And in ionic app you can access it using same ipv4 address i.e 192.168.X.X something like,

this.http.get('http://192.168.X.X:5000', {
      headers: headers
    }).subscribe(data=>{
      console.log();
      this.posts = data;
       });
  }

And when you are going live you have to replace the ipv4 with your hosted flask app's url accordingly.

k33da_the_bug
  • 812
  • 8
  • 16
  • Thanks,i understend. One question, when i will reset(reload)my flask server how i make this? flask app.py not refresh the new data live. – Din14 Feb 25 '22 at 12:01
  • not able to understand – k33da_the_bug Feb 25 '22 at 12:10
  • 0 on my terminal when i have refresh my script data not refres. exp.when i add new data on script i not see news on Android Drive and on PC. – Din14 Feb 25 '22 at 12:24
0

on my terminal when i have refresh my script data not refres.

exp.when i add new data on script i not see news on Android Drive and on PC.

 {
          "id": 5,
          "quantity_left": 5,
          "image": "", 
          "translations": [
            {
                "locale": "sk",
                "title":"Park nový",   
                "description":"Park je nový",
                 
            },
Din14
  • 41
  • 4
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 25 '22 at 12:19
0

I make cloud serve, and work at chrome oder not work on app.

This script is my app.py

from flask import Flask, jsonify,request
from posts import posts


app=Flask(__name__)

@app.route('/')
def index():
    lang = request.headers.get("Accept-Language", 'sk')[:2]

    p = list(map (lambda post: translate(post, lang), posts))

    return jsonify(p)


def translate(post, lang):
    translation = next (t for t in post['translations'] if t['locale'] == lang)

    return {
        "id": post ['id'],
        "title": translation ['title'],
        "description": translation ['description'],
        "image": post['image'],
}


This work on Chrome, not works on app,when I replace my cloud url.

Thanks

Din14
  • 41
  • 4