Curious G.
Curious G.

Reputation: 877

How to fix the hosting address in Flask ngrok

I'm using an API from my computer as a server via flask ngrok, which generates an address where users can remotely use my API, however every time I run my api it generates a random address like this: http://1b1c-187- 121-198-62.ngrok.io How do I generate a fixed address?

This is my main code:

from flask_ngrok import run_with_ngrok
from flask import Flask, flash, request, redirect, url_for, render_template
import os
from werkzeug.utils import secure_filename
from PIL import Image, ImageOps

#app = Flask(__name__)
app = Flask(__name__, template_folder='./templates')
run_with_ngrok(app)   #starts ngrok when the app is run


UPLOAD_FOLDER = './static/'
 
app.secret_key = "secret key"
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024
 
ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg', 'gif'])
@app.route('/')
def index():
    return render_template('index.html')
        
########################################
def allowed_file(filename):
    return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
          
@app.route('/', methods=['POST'])
def upload_image():                       
    if 'file' not in request.files:
        flash('No file part')
        #return redirect(request.url)
    file = request.files['file']
    if file.filename == '':
        flash('No image selected for uploading')
        #return redirect(request.url)
    if file and allowed_file(file.filename):
        filename = secure_filename(file.filename)
        #file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
        file.save(os.path.join(app.config['UPLOAD_FOLDER'], 'foto.jpeg'))

        #### Padronizando tamanho de foto
        basewidth = 400
        # My image is a 200x374 jpeg that is 102kb large
        foo = Image.open('./static/foto.jpeg')
        wpercent = (basewidth/float(foo.size[0]))
        # I downsize the image with an ANTIALIAS filter (gives the highest quality)
        hsize = int((float(foo.size[1])*float(wpercent)))
        foo = foo.resize((basewidth,hsize), Image.ANTIALIAS)
        foo = ImageOps.exif_transpose(foo)
        foo.save("./static/fase1/foto.jpeg",optimize=True,quality=95)
        #foo.save("./static/foto2.jpeg", optimize=True,quality=50)
        #########

        #### Meu código
        #os.remove('./static/foto.png')
        #os.remove('./static/uploads/foto2.png')
        os.system('python evala.py --trained_model=./weights/yolact_plus_resnet50_meat_3700_495900.pth --config=yolact_resnet50_meat_config --score_threshold=0.8 --top_k=100 --images=./static/fase1:./static/mask')
        #### 
        # get directory path where you want to save the images
          #print('upload_image filename: ' + filename)
        flash('Image successfully uploaded and displayed below')
        return render_template('index.html', filename=filename)
    else:
        flash('Allowed image types are - png, jpg, jpeg, gif')
        return redirect(request.url)

@app.route('/display/<filename>')
def display_image(filename):
    return redirect(url_for('static', filename='uploads/foto2.jpeg'), code=301)

app.run()

OBS: My code won't work, it's just to show about the base of my code.

Upvotes: 0

Views: 1809

Answers (1)

dmitrybelyakov
dmitrybelyakov

Reputation: 3864

As mentioned by others before, that requires a paid plan that will give you custom domains and reserver domains.

For a free alternative, you can request a subdomain (-s) with localtunnel.

Upvotes: 2

Related Questions