Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
295 views
in Technique[技术] by (71.8m points)

oop - Flask - inheritance - python's self issue

I have a little flask application, it is mostly written with methods (just one class).

Can someone explain me - example would be great - why I am receiving 1 missing argument after adding self to method declaration?

Here is a code:

from __future__ import print_function
from flask import Flask, render_template, make_response
from flask import redirect, request, jsonify, url_for
from multiprocessing import Value
from flask_classful import FlaskView

#from settings import *
from utils.settings import *

import logging
import linecache
import os
import simplejson as json
import mysql.connector

counter = Value('i', 0)
app = Flask(__name__)


class FlaskCLS(FlaskView):
    app.debug = False
    app._static_folder = os.path.abspath("templates/static/")

    @app.route('/v1/api/cords', methods=['GET', 'POST', 'OPTIONS'])
    def snd_post():
        res = make_response({'x': '55.333', 'y': '18.666'})
        return res

    @app.route('/', methods=['GET', 'POST', 'OPTIONS'])
    def index():
        result = []
        data = request.data
        with counter.get_lock():
            counter.value += 1
            out = counter.value
            with open(str(out), 'a') as dumping:
                json.dump(data, dumping)

# ----------------------parser-section------------
        with open(str(out), 'r+') as file:
            content = file.readlines()
            for line in content:
                result.append(line.replace('\', "").replace('"', "").
                              replace('{', "").replace('}', '').
                              replace(']', '').replace(',','
').
                              replace('[',''))
        f = open(str(out), 'w')
        f.write('')
        f.close()

        with open(str(out), 'a') as appending:
            appending.write('')
            for line in result:
                appending.write(line)

# --------------------cords-grabbing-------------
        x = linecache.getline(str(out), 1)
        y = linecache.getline(str(out), 2)
        fx = open('x', 'w')
        fy = open('y', 'w')

        fx.write(x)
        fx.close()
        fy.write(y)
        fy.close()

        mydb = mysql.connector.connect(
            host=db_host,
            user='root',
            auth_plugin='mysql_native_password',
            password=password,
            buffered=buffered,
            database=database)
        mycursor = mydb.cursor()
        mycursor.execute('select nazwa, x, y, SQRT(POW(69.1 * (x - 54.111), 2) + POW(69.1 *(18.222 - y)
         * COS(x / 57.3), 2)) as distance from wet_dane having distance <125 order by distance;')
        mycursor.fetchall()
        mydb.commit()

# ---------------------empty-files----------------
        str_directory = '/var/www/html/'
        list_files = [x for x in os.listdir(str_directory) if x[0] != '.']
        for each_file in list_files:
            file_path = '%s/%s' % (str_directory, each_file)
            if os.path.getsize(file_path) == 0:
                os.remove(file_path)
            else:
                pass
        return render_template('layouts/index.html')


flask = FlaskCLS()
# ---------------------runner-section-------------
if __name__ == '__main__':
    log = logging.getLogger('werkzeug')
    log.disabled = True
    app.run(host, port, debug, ssl_context=context)

My point is - how properly add self to index and snd_post methods? I want to pass - for example - x variable from index to snd_post in the same class. Without self flask doesn't allow me to do such an operation.

Thanks.

question from:https://stackoverflow.com/questions/66051206/flask-inheritance-pythons-self-issue

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I don't have experience with Flask-Classful, but it looks that you are supposed to register the routes to the app this way:

class QuotesView(FlaskView):
    def index(self):
        return "Hello"

QuotesView.register(app)

Instead of the @app.route(...) decorator.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...