简体   繁体   English

如何在同一页面的特定部分显示Flask表单结果

[英]How to display Flask form results on a specific part of the same page

CONTEXT CONTEXT

I've created a flask application that contains a form on the homepage. 我已经在主页上创建了一个包含表单的烧瓶应用程序。 The user is meant to download and re-upload a filled-out template file, then fill out the form. 用户打算下载并重新上传已填写的模板文件,然后填写表单。 When this form is submitted a python script is invoked on uploaded file on the backend, which finally produces an .html file. 提交此表单时,会在后端上传的文件上调用python脚本,最终生成.html文件。

DESIRED RESULT 期望的结果

Originally, I was having this resulting .html file displayed on different URL (via a redirect), but what I'd like instead is for the form to be displayed on the left half of the homepage, and the html result to display right next to it, on the right half of the homepage. 最初,我将这个生成的.html文件显示在不同的URL上(通过重定向),但我想要的是将表单显示在主页的左半部分,并将html结果显示在下一个在主页的右半边。

I'm struggling with how to achieve this, especially in specifying where on the page I'd like the results to appear, and suspect I need to use JavaScript (which I am unfamiliar with). 我正在努力解决如何实现这一点,特别是在指定页面上我希望结果出现的位置,并怀疑我需要使用JavaScript(我不熟悉)。 I tried the "anchor" approach but that did not work for me either. 我尝试了“锚”方法,但这对我也没有用。 I'd really appreciate any guidance with this! 我真的很感激任何指导! Thanks :) 谢谢 :)

CODE

app.py: app.py:

def upload():
    if request.method == 'POST':
            if request.form['affiliation'] == "letter":
                affiliation = "let"
            elif request.form['affiliation'] == "number":
                affiliation = "num"

            proc = subprocess.Popen('python author_script.py {} -m {}'.format(file.filename, affiliation), shell=True, stdout=subprocess.PIPE)
            time.sleep(0.5) #wait for html file to be created before going to the results page.
            return redirect(url_for('upload'))

    return render_template('index.html', template_file=app.config['TEMPLATE_FILE'])

index.html: index.html的:

{% extends "layout.html" %}

{% block body %}

<div class="container">
    <div style="text-align:left">
      <br>
      <h1>Author List Script</h1>
    </div>
    <section id="intro" class="main">
    <div class="row">
      <div class="column">
          <h6>Download the template file below and re-upload with your custom author information</h6><br>
          <a href="static/ExampleAuthorList.txt" download="Authors_Template.txt"><button type="button">Download</button></a><br><br><br>
          <form action="" method=post enctype=multipart/form-data>
          <div id="buttonDiv">
            <p><input type=file name=file value="Choose File">
            <p>Mark affiliations with:</p>
            <input type="radio" name="affiliation" value="number" id="number" class="form-radio" checked><label for="number" checked>Number</label><br>
            <input type="radio" name="affiliation" value="letter" id="letter" class="form-radio"><label for="letter">Letter</label>
            <br><br>
          </div>
            <input type=submit value=Upload></p>
          </form>
          </div>
      <div class="column">
          <h4>Results</h4>
          <!-- Where I want the results to appear -->
      </div>
      </div>
      </section>
    </div>

    <!-- Scripts -->
      <script src="js/jquery.min.js"></script>
      <script src="js/skel.min.js"></script>
      <script src="js/util.js"></script>
      <script src="js/main.js"></script>

{% endblock %}

UPDATE UPDATE

I understand how to divide the homepage into columns, but I still don't understand how to specify that I want the python-produced .html (called authorList.html ) file to display in the Where I want the results to appear part of index.html . 我理解如何将主页划分为列,但我仍然不明白如何指定我希望python生成的.html (称为authorList.html )文件显示在Where I want the results to appearindex.html一部分index.html

Previously, I had it set up so that the upload route would redirect to the results route, and then the results route simply renders the resulting authorList.html . 以前,我已将其设置为upload路由将重定向到results路由,然后results路由只是呈现生成的authorList.html But how can I specify within a specific part of index.html where I want the template to be rendered? 但是,我如何在index.html的特定部分中指定我想要呈现模板的位置? Do I need to do something like <a href="authorList.html"> within index.html ? 我是否需要在index.html执行<a href="authorList.html">之类的操作?

Previous code 以前的代码

@app.route("/", methods=['GET', 'POST'])
def upload():
    if request.method == 'POST':
            if request.form['affiliation'] == "letter":
               affiliation = "let"
            elif request.form['affiliation'] == "number":
               affiliation = "num"

            proc = subprocess.Popen('python author_script.py {} -m {}'.format(file.filename, affiliation), shell=True, stdout=subprocess.PIPE)
            time.sleep(0.5) #wait for html file to be created before going to the results page.
            return redirect(url_for('results'))
    return render_template('index.html', template_file=app.config['TEMPLATE_FILE'])

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

UPDATE 2: MY ATTEMPT 更新2:我的尝试

I tried using a JQuery function to include the authorList.html within index.html (based on this question here: Include another HTML file in a HTML file ), but when I press the "Upload" button, the Results section of my homepage is still blank. 我尝试使用JQuery函数在index.html包含authorList.html (这里基于这个问题: 在HTML文件中包含另一个HTML文件 ),但是当我按下“上传”按钮时,我的主页的结果部分是还是空白。 Maybe the redirect is somehow messing things up? 也许重定向在某种程度上搞乱了?

app.py: app.py:

def upload():
    if request.method == 'POST':
            if request.form['affiliation'] == "letter":
                affiliation = "let"
            elif request.form['affiliation'] == "number":
                affiliation = "num"

            proc = subprocess.Popen('python author_script.py {} -m {}'.format(file.filename, affiliation), shell=True, stdout=subprocess.PIPE)
            time.sleep(0.5) #wait for html file to be created before going to the results page.
            return redirect(url_for('upload'))

    return render_template('index.html', template_file=app.config['TEMPLATE_FILE'])

index.html: index.html的:

{% extends "layout.html" %}

<head>

  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
  <script src="jquery.js"></script> 
  <script> 
    $(function(){
      $("#includedContent").load("authorList.html"); 
    });
  </script> 

</head>

{% block body %}

<div class="container">
  <div class="row">

    <div class="col-sm-6">

<div style="text-align:left"><br><h1>Author List Script</h1></div>

    </div>


    <div class="col-sm-6">

       <section id="intro" class="main">
    <div class="row">
      <div class="column">
          <h6>Download the template file below and re-upload with your custom author information</h6><br>
          <a href="static/ExampleAuthorList.txt" download="Authors_Template.txt"><button type="button">Download</button></a><br><br><br>
          <form action="" method=post enctype=multipart/form-data>
          <div id="buttonDiv">
            <p><input type=file name=file value="Choose File">
            <p>Mark affiliations with:</p>
            <input type="radio" name="affiliation" value="number" id="number" class="form-radio" checked><label for="number" checked>Number</label><br>
            <input type="radio" name="affiliation" value="letter" id="letter" class="form-radio"><label for="letter">Letter</label>
            <br><br>
          </div>
            <input type=submit value=Upload></p>
          </form>
          </div>
      <div id="includedContent" class="column">
          <h4>Results</h4>
      </div>
      </div>
      </section>


    </div>

  </div>
</div>

{% endblock %}

This is not flask question. 这不是烧瓶问题。 it can easily be achieve using css module like bootstraps and jquery 它可以很容易地使用像bootstraps和jquery这样的css模块来实现

Css Class via row and col-sm is what does the job for you. 通过col-sm的 Css类是你的工作。 col-sm has maximum value of 12 which represents size of a full page width. col-sm的最大值为12 ,表示整页宽度的大小。

If you want three columns where you can place your results and data, you can split col-sm into three ie col-sm-4 as 4+4+4 will give you 12 如果你想要列你可以放置你的结果和数据,你可以将col-sm分成三个,即col-sm-4,因为4 + 4 + 4会给你12

If you want two columns where you can place your results and data, just like you have ask, you can split col-sm into two ie col-sm-6 as 6+6 will give you 12 如果你想要两列你可以放置你的结果和数据,就像你问的那样,你可以将col-sm分成两个,即col-sm-6,因为6 + 6会给你12

Three column Examples 三栏实例

<!DOCTYPE html>
<html lang="en">
<head>

  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
</head>
<body>


<div class="container">
  <div class="row">
    <div class="col-sm-4">
      <h3>Left</h3>
          <div>Your content for left appears here</div>
    </div>
    <div class="col-sm-4">
      <h3>middle</h3>
           <div>Your content for Middle appears here</div>

    </div>
    <div class="col-sm-4">
      <h3>Right</h3>        
      <div>Your content for right appears here</div>

    </div>
  </div>
</div>

</body>
</html>

2 column Examples 2列示例

<!DOCTYPE html>
<html lang="en">
<head>

  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
</head>
<body>


<div class="container">
  <div class="row">
    <div class="col-sm-6">
      <h3>Left</h3>
          <div>Your content for left appears here</div>
    </div>

    <div class="col-sm-6">
      <h3>Right</h3>        
      <div>Your content for right appears here</div>

    </div>
  </div>
</div>

</body>
</html>

To fix your content be mindful of where Div opens and closes 要修复您的内容,请注意Div打开和关闭的位置

<!DOCTYPE html>
<html lang="en">
<head>

  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
</head>
<body>


<div class="container">
  <div class="row">



    <div class="col-sm-6">

      <h3>Left for author script</h3>
<div style="text-align:left"><br><h1>Author List Script</h1></div>

    </div>


    <div class="col-sm-6">

      <h3>Main Content at Right</h3>        
       <section id="intro" class="main">
    <div class="row">
      <div class="column">
          <h6>Download the template file below and re-upload with your custom author information</h6><br>
          <a href="static/ExampleAuthorList.txt" download="Authors_Template.txt"><button type="button">Download</button></a><br><br><br>
          <form action="" method=post enctype=multipart/form-data>
          <div id="buttonDiv">
            <p><input type=file name=file value="Choose File">
            <p>Mark affiliations with:</p>
            <input type="radio" name="affiliation" value="number" id="number" class="form-radio" checked><label for="number" checked>Number</label><br>
            <input type="radio" name="affiliation" value="letter" id="letter" class="form-radio"><label for="letter">Letter</label>
            <br><br>
          </div>
            <input type=submit value=Upload></p>
          </form>
          </div>
      <div class="column">
          <h4>Results</h4>
          <!-- Where I want the results to appear -->
      </div>
      </div>
      </section>


    </div>





  </div>
</div>

</body>
</html>

You can now add other things that follows and make the code compatible with your applications 您现在可以添加其他内容并使代码与您的应用程序兼容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM