Accessing file via URL - python-3.x

I deployed a python flask app on Heroku and it works fine. I have also uploaded a well-known folder that contains an apple-app-site association file, but when I try to access the file via URL, it returns 404. Basically, I'm testing universal links, so I wanted the apple-app-site association to be accessible. When I checked the status in the branch.io aasa validator, it returns server error, please find attached the screenshot Screenshot
What I'm doing wrong?
main.py
from flask import Flask
app = Flask(__name__, static_url_path='/well-known')
#app.route('/')
def index():
return "<h1>Welcome to Universal Link - iOS<h1>"
wsgi.py
from app.main import app
if __name__ == "__main__":
app.run()

Related

Listen on given path using python

I am new to python basically i work in Infra.
To test one of AWS service i have written below python code which listen "/ping" GET method and "/invocations" POST method.
from flask import Flask, Response
app = Flask(__name__)
#app.route("/ping",methods=["GET"])
def ping():
return Response(response="ping endpoint", status=200)
#app.route("/invocations",methods=["POST"])
def predict():
return Response(response="invocation endpoint here", status=200)
if __name__ == "__main__":
print("Traninig started")
app.run(host="localhost", port=8080)
This code works fine but Flask give warning like "Dont use in production".
So i was looking to do the same in Django but not able to find appropriate ways to achieve this.Not sure what i am trying to do is even doable but i hope i made it clear what i am trying to do.
Use Gunicorn server for your flask projects in production

How to run function from web app locally and not on host

I'm want to create a web app so a user can run process locally on his computer (I know that currently it doesn't have a real usage).
So I use flask to create the web app and created a button that calls function that run the process.
Issue is the func runs on the server and not on the machine the user use.
For example to be clear: Server runs on machine 1.2.3.4, a user log the web app from a different machine 5.6.7.8 and press the button, the func will run on the host (1.2.3.4) causing the process to open on the host and not locally on user computer.
from flask import Flask, render_template, request, url_for, flash, redirect
import subprocess
def runchrome():
p = subprocess.Popen(r"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe")
app = Flask(__name__)
#app.route("/home", methods=["GET", "POST"])
def home():
if request.method == "POST":
process = request.form["process"]
if process == "Chrome":
runchrome()
return render_template("Home.html")
You cannot run python code on the client's computer via a webapp in this way. Otherwise everybody could abuse this and run arbitrary code on computers that visit their websites. However, you can run javascript on the client's computer inside their browser.
For this to happen, you need to return a valid html document containing javascript. See this tutorial as a starting point: https://www.jitsejan.com/python-and-javascript-in-flask.html
Another way would be that the user installs your app on their machine.

How can I deploy my API on IBM Cloud developed in Python and swagger?

I'm developing an API for our back-end, using python 3 and swagger + connexion (I just followed this great tutorial https://realpython.com/flask-connexion-rest-api/#using-connexion-to-add-a-rest-api-endpoint).
I was succesfully created my own API, when I run it locally the swagger ui perfectly appears (using this http://127.0.0.1:5000/api/ui/).
My problem is when I deploy it on IBM Cloud when I'm trying to access it, i have this error: 404 Not Found: Requested route does not exist).
Please see below my sample application code, my default python file.
# Sample flask app to connect to our backend
# __author__ = 'paulcaballero'
from flask import Flask, render_template
import data_connection as dc
import os
import connexion
from flask_restplus import Resource, Api
APP_ROOT = os.path.dirname(os.path.abspath(__file__))
#Create the application instance
app = connexion.App(__name__, specification_dir='./')
# Read the swagger.yml file to configure the endpoints
app.add_api('swagger.yml')
# If we're running in stand alone mode, run the application
port = os.getenv('PORT', '5000')
if __name__ == "__main__":
`app.run(host='0.0.0.0', port=int(port))
I want my default page to show all my endpoints.

Google Cloud Console - Flask - main.py vs package

OK, so I have been through some tutorials to get a flask app onto google cloud, which is fine.
I have also been through the flask tutorial to build a flaskr blog:
http://flask.pocoo.org/docs/1.0/tutorial/
It occurred to me that a sensible thing to do would be to create a database (MySQL in mycase) on google and then modify the code so that it uses that. This is fine and I can get it to work on my local machine.
However, now that I am coming to deploying this, I have hit a problem.
The google cloud tutorials tend to use a flask app that is initiated in a single file such as main.py, eg:
from flask import Flask, render_template
app = Flask(__name__)
....
The flask tutorial mentioned above uses a package and puts the code to create_app() in the __init__.py file and at present I cannot get this to start in the same way. (see sample code).
from flask import Flask
def create_app(test_config=None):
# create and configure the app
app = Flask(__name__, instance_relative_config=True)
app.config.from_mapping(
SECRET_KEY='dev'
)
Are there some adjustments that I need to make to something like the app.yaml file to get it to recognise flask as the flaskr package or do I need to rewrite the whole thing so that it uses a main.py file ?
I feel that this is one of the points in time where I could really pick up a bad habit. What in general is the preferred way to write flask apps on google cloud ?
I am using the standard environment in google.
Thanks for your advice.
Mark
Since you have an application factory, you can create the app anywhere. Just create it in main.py, since this is what App Engine expects:
from my_package import create_app
app = create_app()

Flask + Bokeh Server on Azure Web App Service

I want to host my bokeh server app in Azure Web App Services. Following the example in flask_embed.py I created a minimal example with a bokeh server process running on localhost:5006 and serving it with server_document in a flask route. Locally, in my computer, it runs normally without any errors:
from threading import Thread
from bokeh.embed import server_document
from bokeh.server.server import Server
from bokeh.models.widgets import Select, Div
from bokeh.layouts import column
from flask import Flask
from flask import render_template
from tornado.ioloop import IOLoop
app = Flask(__name__)
# This is the bokeh page
def modify_doc(doc):
dropdown = Select(title="Cities", options=["New York", "Berlin"])
title_row = Div(text="Home Page")
main_layout = column([
title_row,
dropdown
])
doc.add_root(main_layout)
doc.title = "My bokeh server app"
# This is the subprocess serving the bokeh page
def bk_worker():
server = Server(
{'/bkapp': modify_doc},
io_loop=IOLoop(),
allow_websocket_origin=["*"],
)
server.start()
server.io_loop.start()
Thread(target=bk_worker).start()
# This is the flask route showing the bokeh page
#app.route("/", methods=["GET"])
def my_app():
script = server_document("http://localhost:5006/bkapp")
return render_template("embed.html", script=script, template="Flask")
However, when I push it to the Azure web app, the page is blank and by inspecting the page an error message is shown:
GET https://<my-azure-site>.azurewebsites.net:5006/bkapp/autoload.js?bokeh-autoload-element=0bfb1475-9ddb-4af5-9afe-f0c4a681d7aa&bokeh-app-path=/bkapp&bokeh-absolute-url=https://<my-azure-site>.azurewebsites.net:5006/bkapp net::ERR_CONNECTION_TIMED_OUT
It seems like I don't have access to the localhost of the remote Azure server. Actually, it's not yet clear to me if the bokeh server runs/is allowed to run at all. In the server_document function I have tried putting server_document("<my-azure-site>:5006/bkapp") but the problem remains the same.
Any help is appreciated.
This post is related to another question: Bokeh embedded in flask app in azure web app
I realize this is from a while ago, but I've spent many hours in the past several days figuring this out, so this is for future people:
The issue is that server_document() is just creating a <script> tag that gets embedded into a jinja2 template, where it executes.
Locally it's not an issue because your bokeh server is running on YOUR MACHINE'S localhost:5006. To demonstrate, you can see that you can navigate directly to localhost:5006/bkapp to see your bokeh document.
Once you're hosting it on Azure, server_document() is creating the exact same script that a browser will try to execute - that is, your browser is going to try to execute a <script> tag that references localhost:5006, except that there isn't anything running on localhost:5006 because your bokeh app is actually running on Azure's server now.
I'm not sure what the best way to do it is, but the essence of it is that you need server_document() to point to the bokeh server that's running remotely. To do this you'll need to make sure that {your_remote_bokeh_server}:5006 is publicly accessible.

Resources