无法正确显示html的数据帧

2024-04-19 21:49:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图呈现模板,这是为了显示一个页面上的数据帧。它显示的是数据帧,但它在数据帧周围添加了奇怪的模式,如下所示
enter image description here
我希望dataframe看起来不错,表内外都有适当的填充。你知道吗

这就是我如何呈现模板。呈现result.html
时出现问题

@app.route('/company-recommendation', methods=['GET', 'POST'])
def company_recommendation():
    if request.method == 'POST':  # this block is only entered when the form is submitted
        try:
            title = request.form.get('title')
            w_descrip = int(request.form.get('descrip'))
            w_indust = int(request.form.get('indust'))
            w_type = int(request.form.get('type'))
            w_size = int(request.form.get('size'))
            w_specs = int(request.form.get('specs'))

            calculate_all_scores(title, df_work, w_descrip, w_indust, w_type, w_size, w_specs)
            result = get_recommendations(df_work)
            new_df = df_work.loc[[result[0], result[1], result[2], result[3], result[4]]]
            new_df.index = new_df.index.map(lambda x: str(x).upper())
            result = new_df[['spec_scores', 'industry_scores', 'type_scores', 'cosine_scores','size_scores', 'total_scores','type', 'industry', 'specialties']]

            pd.set_option('display.max_colwidth', -1)
            tables = [result.to_html(classes='table table-hover', header="true")]
            return render_template("result.html", result=tables, name=title.upper())
            #return render_template("result.html", result=result, name=title.upper())
        except Exception as e:
            error = 'Exception Occurred : '+str(e)+' \nGo back and try again !!'
            return error

    return render_template("form.html")


result.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">

    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Company Similarity</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> 
    <style type="text/css">
        h1{
            margin: 30px 0;
            padding: 0 200px 15px 0;
            border-bottom: 1px solid #E5E5E5;
        }
        h4{
            margin: 30px 0;
            padding: 0 200px 15px 0;
            border-bottom: 1px solid #E5E5E5;
        }
        .bs-example{
            margin: 20px;
        }

    </style>

</head>


<body>
    <div class="bs-example">
        <h1> RECOMMENDED COMPANIES </h1>
        <h4> Best 5 Matches for {{name}}</h4>
        <div class="table-responsive">
            {{result | safe}}
        </div>

    </div>
</body>
</html>

Tags: nameformdfnewsizegetreturntitle