pandas 如何使用Session在不同路由之间传递flask应用程序中的变量

p8ekf7hl  于 2023-05-12  发布在  其他
关注(0)|答案(1)|浏览(106)

我想使用Session将上传路由中的两个变量传递给classify_data路由。但是,我得到了一个错误。错误是:TypeError:process_data()接受0个位置参数,但给出了2个。
这是我的flask app的代码。

import csv
from flask import Flask, render_template, request, redirect, url_for, session
import pandas as pd
from scraper import scrape_data
from data_cleaning import clean_data
from data_preprocessing import process_data
from flask_session import Session

app = Flask(__name__)
app.static_folder = 'static'
app.secret_key = 'fyp'
app.config['SESSION_PERMANENT'] = False
app.config['SESSION_TYPE'] = "filesystem"
Session(app)

@app.route('/')
def home():
    return render_template('home_page.html')

@app.route('/upload_page')
def upload_page():
    return render_template('upload_page.html')

@app.route('/upload', methods=['POST'])
def upload():
    job_field = request.form['job-field']
    job_title = request.form['job-title']
    jobdata = []

    if job_field == "Information Technology and Software Development":
        if job_title == "Senior Frontend Developer":
            with open('static/job_IT.csv', newline='') as csvfile:
                reader = csv.reader(csvfile, delimiter=',', quotechar='"')
                next(reader)  # skip the header row
                for i, row in enumerate(reader):
                    if i == 1:  # append the second row to the data list
                        jobdata.append(row)
                        break  # stop reading the file
        elif job_title == "Data Processing Specialist":
            with open('static/job_IT.csv', newline='') as csvfile:
                reader = csv.reader(csvfile, delimiter=',', quotechar='"')
                next(reader)  # skip the header row
                for i, row in enumerate(reader):
                    if i == 2:  # append the second row to the data list
                        jobdata.append(row)
                        break  # stop reading the file
        elif job_title == "Java Developer":
            with open('static/job_IT.csv', newline='') as csvfile:
                reader = csv.reader(csvfile, delimiter=',', quotechar='"')
                next(reader)  # skip the header row
                for i, row in enumerate(reader):
                    if i == 3:  # append the second row to the data list
                        jobdata.append(row)
                        break  # stop reading the file
    elif job_field == "Digital Marketing":
        if job_title == "Digital Marketing Web Specialist":
            with open('static/job_DM.csv', newline='') as csvfile:
                reader = csv.reader(csvfile, delimiter=',', quotechar='"')
                next(reader)  # skip the header row
                for i, row in enumerate(reader):
                    if i == 1:  # append the second row to the data list
                        jobdata.append(row)
                        break  # stop reading the file
        elif job_title == "Video Editor":
            with open('static/job_DM.csv', newline='') as csvfile:
                reader = csv.reader(csvfile, delimiter=',', quotechar='"')
                next(reader)  # skip the header row
                for i, row in enumerate(reader):
                    if i == 2:  # append the second row to the data list
                        jobdata.append(row)
                        break  # stop reading the file
        elif job_title == "Graphic Designer":
            with open('static/job_DM.csv', newline='') as csvfile:
                reader = csv.reader(csvfile, delimiter=',', quotechar='"')
                next(reader)  # skip the header row
                for i, row in enumerate(reader):
                    if i == 3:  # append the second row to the data list
                        jobdata.append(row)
                        break  # stop reading the file
    elif job_field == "Finance":
        if job_title == "Financial Analyst-SSC":
            with open('static/job_FN.csv', newline='') as csvfile:
                reader = csv.reader(csvfile, delimiter=',', quotechar='"')
                next(reader)  # skip the header row
                for i, row in enumerate(reader):
                    if i == 1:  # append the second row to the data list
                        jobdata.append(row)
                        break  # stop reading the file
        elif job_title == "Accountant":
            with open('static/job_FN.csv', newline='') as csvfile:
                reader = csv.reader(csvfile, delimiter=',', quotechar='"')
                next(reader)  # skip the header row
                for i, row in enumerate(reader):
                    if i == 2:  # append the second row to the data list
                        jobdata.append(row)
                        break  # stop reading the file
        elif job_title == "Specialist, Business Finance (APAC)":
            with open('static/job_FN.csv', newline='') as csvfile:
                reader = csv.reader(csvfile, delimiter=',', quotechar='"')
                next(reader)  # skip the header row
                for i, row in enumerate(reader):
                    if i == 3:  # append the second row to the data list
                        jobdata.append(row)
                        break  # stop reading the file

    file = request.files['file']
    df = pd.read_csv(file)
    data = scrape_data(df)
    cleandata = clean_data(data)
    session['cleandata'] = cleandata
    session['jobdata'] = jobdata

    return render_template('display.html', data=cleandata, jobdata=jobdata)

@app.route('/classify_data', methods=['POST'])
def process_data():
    cleandata = session.get('cleandata')
    jobdata = session.get('jobdata')
    processdata = process_data(cleandata, jobdata)
    print(processdata)
    return render_template('display_classification.html', processdata=processdata)

if __name__ == '__main__':
    app.run(debug=True)

下面是我的python文件的代码:`` import csv import pandas as pd from sklearn.feature_extraction.text import CountVectorizer from sklearn.metrics.pairwise import cosine_similarity
def process_data(cleandata,jobdata):

headers = cleandata[0].keys()
with open('data.csv', 'w', newline='') as f:
    writer = csv.DictWriter(f, fieldnames=headers)
    writer.writeheader()
    for row in cleandata:
        writer.writerow(row)

headers = jobdata[0].keys()
with open('jobdata.csv', 'w', newline='') as f:
    writer = csv.DictWriter(f, fieldnames=headers)
    writer.writeheader()
    for row in jobdata:
        writer.writerow(row)

# read data from csv files
df1 = pd.read_csv('data.csv')
df2 = pd.read_csv('jobdata.csv')

# 1. FIND SIMILARITY SCORE
# select the columns to compare
columns_to_compare = ['Title', 'Education', 'Skills', 'Languages']

# iterate over each row in df1 and calculate similarity score with df2
similarity_scores = []
for idx, row in df1.iterrows():
    text1 = ' '.join(str(row[col]) for col in columns_to_compare)
    text2 = ' '.join(str(val) for val in df2.iloc[0][columns_to_compare])

    # create a bag-of-words representation of the text
    vectorizer = CountVectorizer(binary=True, stop_words=None, min_df=1)
    X = vectorizer.fit_transform([text1])
    Y = vectorizer.transform([text2])

    # calculate cosine similarity between the bag-of-words representations
    similarity_score = cosine_similarity(X, Y)[0][0]
    similarity_scores.append(similarity_score)

# add the similarity scores to the original dataframe
df1['similarityscores_title_edu_skills_lang'] = similarity_scores

# save the dataframe to csv file
df1.to_csv('similarity_scores.csv', index=False)

process = []

for item in cleandata:
    process_item = {}
    for key, value in item.items():
        process_item[key] = value
    process.append(process_item)

# 2. calculate duration (for experiences)
# 3. label data -> then display (sekali rate kot)
# 4. split column

return process
3pmvbmvn

3pmvbmvn1#

您的flask应用程序导入该函数:

from data_preprocessing import process_data

...但随后它会用另一个同名函数覆盖导入的函数:

@app.route('/classify_data', methods=['POST'])
def process_data():
    ...

将第二个函数命名为其他名称。

相关问题