各位大神您好,
我當前寫了一隻簡單的 Python Flask 程式與利用 R plumber寫了一個 api,
想在 Flask 的網頁中顯示 R api的圖形,結果只能在本地中顯示,只要是外部網路
連線,圖形就會顯示無法連線,想請教如何解決呢?
目前想法可能需要在 Flask 下呼叫 R API 再送到前端頁面中,但不知道如何下手,
希望能找個方向或者範例。
python: flask code
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
if __name__ == "__main__":
app.run(host="0.0.0.0", port=8080)
html: index code
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
<title>Hello, world!</title>
</head>
<body>
<!-- 引入 R api -->
<div class="embed-responsive embed-responsive-16by9">
<iframe class="embed-responsive-item" src="127.0.0.1:8000/plot" allowfullscreen></iframe>
</div>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js" integrity="sha384-9/reFTGAW83EW2RDu2S0VKaIzap3H66lZH81PoYlFhbGU+6BZp6G7niu735Sk7lN" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV" crossorigin="anonymous"></script>
</body>
</html>
<!-- 引入 R api -->
<div class="embed-responsive embed-responsive-16by9">
<iframe class="embed-responsive-item" src="127.0.0.1:8000/plot" allowfullscreen></iframe>
</div>
这里src 应该是r api 地址吧,你外部访问的话 应该也要改成对应外部的地址。