[英]Flask extract value from drop down menu if value is url_for()
I want to be able to serve a static html file (located in static/{platform}/graph.html
) within an iframe
based on the value ( {platform}
) selected from a drop down menu.我希望能够根据从下拉菜单中选择的值 ( {platform}
) 在iframe
中提供 static html 文件(位于static/{platform}/graph.html
)。 I also want to use the value selected in drop down menu in other places as well.我也想在其他地方使用在下拉菜单中选择的值。
Right now I have something that works for serving static html file in the iframe and updating it based on drop down menu value, but the problem is that I'm unable to use the value from the drop down menu elsewhere.现在我有一些东西可以在 iframe 中提供 static html 文件并根据下拉菜单值更新它,但问题是我无法在其他地方使用下拉菜单中的值。
I think it's because the value is a url_for()
, but I'm not sure.我认为这是因为该值是url_for()
,但我不确定。
@app.route('/', methods=['GET'])
def index():
return render_template('index.html')
@app.route('/maps', methods=['GET','POST'])
def maps():
print(request.form.get('location')) ## This prints None
return render_template('maps.html')
@app.route('/static/<string:platform>/graph.html', methods=['GET', 'POST'])
def show_plot(platform):
print(request.form.get('location')) ## This prints None
try:
return send_file('static/{platform}/graph.html'.format(platform=platform))
except FileNotFoundError:
print("Error")
<head>
<!-- define iframe src based on drop down menu value -->
<script type="text/javascript">
function setIframeSource() {
var theSelect = document.getElementById('location');
var theIframe = document.getElementById('plot');
var theUrl;
theUrl = theSelect.options[theSelect.selectedIndex].value;
theIframe.src = theUrl;
}
</script>
</head>
<body>
<!-- drop down menu -->
<form id="select-platform" method="post">
<label> Select Platform</label>
<select name="location" id="location" onchange="setIframeSource()">
<option value="{{ url_for('show_plot', platform='web') }}">Web</option>
<option value="{{ url_for('show_plot', platform='app') }}">App</option>
</select>
</form>
<!-- embed static html file based on drop down menu value -->
<div class="iframe-container">
<iframe
id="plot"
onload='javascript:resizeIframe(this);'
style="border:0"
src="{{ url_for('show_plot', platform='web') }}"
>
</iframe>
</div>
</body>
Adding a submit
button that made a POST
request to a new _refresh_plot
endpoint allowed me to see the URL from the url_for()
value from drop down menu value添加一个向新的_refresh_plot
端点发出POST
请求的submit
按钮允许我从下拉菜单值的url_for()
值中看到 URL
@app.route('/_refresh_plot', methods=['POST'])
def _refresh_plot():
pattern = '\/static\/([\w]+)\/graph\.html'
url = request.form.get('location')
platform = re.search(pattern, url)[1]
print(platform)
<form id="select-platform" method="post" action="{{ url_for('_refresh_plot') }}>
<label> Select Platform</label>
<select name="location" id="location" onchange="setIframeSource()">
<option value="{{ url_for('show_plot', platform='web') }}">Web</option>
<option value="{{ url_for('show_plot', platform='app') }}">App</option>
</select>
<button type="submit">Refresh</button>
</form>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.