簡體   English   中英

燒瓶散景服務器 - 人物甚至在本地也不渲染

[英]flask bokeh server - figure does not render even locally

我無法在flask beind apache 中運行bokeh 服務器,所以現在我嘗試在本地提供flask 中的bokeh。 該圖不呈現。 這是燒瓶代碼:

from flask import Flask, render_template
app = Flask(__name__)

from bokeh.embed import server_document
@app.route("/")
def techblog():
    try:
        tag = server_document(url=r'/bokeh', relative_urls=True)
        return render_template('techblog.html', tag=tag)
    except Exception as e:
        return str(e)

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

這是散景代碼:

from numpy.random import random
from bokeh.io import curdoc
from bokeh.plotting import figure
from bokeh.layouts import column, widgetbox
from bokeh.models import Button, ColumnDataSource
from bokeh.server.server import Server

def run(doc):

    fig = figure(title='random data', width=400, height=200, tools='pan,box_zoom,reset,save')

    source = ColumnDataSource(data={'x': [], 'y': []})
    fig.line('x', 'y', source=source)

    def click(n=100):
        source.data = {'x': range(n), 'y': random(n)}

    button = Button(label='update', button_type='success')
    button.on_click(click)

    layout = column(widgetbox(button), fig)
    doc.add_root(layout)
    click()

# configure and run bokeh server
kws = {'port': 5000, 'prefix':'/bokeh','allow_websocket_origin': ['127.0.0.1']}
server = Server(run, **kws)
server.start()
# if __name__ == '__main__':
server.io_loop.add_callback(server.show, '/')
server.io_loop.start()

這是我的 html 模板:

<h1 style='color:blue'>Hello There!</h1>

</br>
{{ tag|safe }}
</br>
{{ tag }}

我正在通過 python 運行燒瓶應用程序。 在單獨的命令處理器上,我通過以下方式運行散景應用程序,

bokeh serve --allow-websocket-origin=localhost:5000 filename.py

我只得到沒有“安全”的標簽

<script src="/bokeh/autoload.js?bokeh-autoload-element=1001&bokeh-app-path=/bokeh" id="1001"></script>

我在燒瓶控制台上有這條消息。 這是一個標准的 404:

"GET /bokeh/autoload.js?bokeh-autoload-element=1000&bokeh-app-path=/bokeh HTTP/1.1" 404 -

就這些。 不呈現圖形或按鈕。 我應該改變什么才能看到這個圖?

編輯:我已經在散景代碼中指定了端口和前綴。 結果沒有改變。

編輯 2:我在燒瓶控制台上為 404 錯誤添加了控制台 msj。

有幾件事是錯誤的:

  • 您尚未配置端口,因此 Bokeh 服務器將使用其默認端口 5006(而非 5000)

  • 您尚未配置應用程序路徑,因此 Bokeh 服務器將從其默認位置/ (不是/bokeh )為應用程序提供服務

還值得一提的是,如果您將localhost列入白名單作為允許 websocket 來源,那么它實際上必須是 URL 欄中的localhost (即不是 127.0.0.1,在這種情況下它們不可互換)

最后,將 Bokeh 應用程序代碼放在一個手動調用Server的普通 python 腳本中需要做很多額外的工作。您可以將run的內容放在一個文件中並調用bokeh serve --port=5000 app.py和然后該應用程序將在localhost:5000/app可用

我在@bigreddot 的回答中添加了一些細節。


代碼對我也不起作用。 我什至在一些教程中找到了這個代碼,主要問題是它使用的是nginx/bokeh轉換為http://127.0.0.1/bokeh 但是在沒有nginx本地計算機上,我必須更改所有 url。

編輯:我找到了帶有此代碼的教程https://rbtechblog.com/blog/deploy_bokeh_app


我開始更改代碼並減少它以創建最少的代碼。 我做了類似於 bigreddot 提到的更改的更改。


散景

我將代碼直接放在沒有defServer文件中

文件名.py

from numpy.random import random
from bokeh.io import curdoc
from bokeh.plotting import figure
from bokeh.layouts import column, widgetbox
from bokeh.models import Button, ColumnDataSource

def click(n=100):
    source.data = {'x': range(n), 'y': random(n)}

fig = figure(title='random data', width=800, height=400, tools='pan,box_zoom,reset,save')

source = ColumnDataSource(data={'x': [], 'y': []}) # place for data
fig.line('x', 'y', source=source)  # draw plot

button = Button(label='update', button_type='success') # create button
button.on_click(click) # assign function to button

layout = column(fig, widgetbox(button)) # create layout
curdoc().add_root(layout) # add all to document

click() # generate random data at start

現在我可以在控制台中運行它

 bokeh serve filename.py 

我可以使用 url 在網絡瀏覽器中看到它

 http://localhost:5006/filename

bokeh應該在啟動后在控制台中顯示此 url - 如果您將使用不同的文件或選項,那么您可能會看到不同的 url)

目前我不需要任何其他選項,但稍后我將需要--allow-websocket-origin但我稍后會描述它。

順便說一句:我不使用名稱bokeh.py因為導入原始bokeh可能會出現問題。


燒瓶

因為我不使用可以將/bokeh轉換為http://localhost:5006/filename nginx所以我必須在serve_document使用完整的 url

對於測試,我使用了render_template_string而不是render_template因此我不必創建templates/index.html以便更容易復制和測試代碼。

如果出現錯誤,我刪除了try/except以獲取更多詳細信息。

應用程序

from flask import Flask, render_template, render_template_string
from bokeh.embed import server_document

app = Flask(__name__)

@app.route("/")
def index():
    tag = server_document(url='http://localhost:5006/filename')
    #return render_template('index.html', tag=tag)
    return render_template_string('''<div>{{ tag|safe }}</div>''', tag=tag)

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

現在我可以運行它

python app.py

我可以使用標准網址在網絡瀏覽器中打開頁面

http://localhost:5000/

但我不會看到情節和bokeh會顯示

Refusing websocket connection from Origin 'http://127.0.0.1:5000'; 
use --allow-websocket-origin=127.0.0.1:5000 
or set BOKEH_ALLOW_WS_ORIGIN=127.0.0.1:5000 to permit this; 
currently we allow origins {'localhost:5006'}

所以我必須用這個選項重新啟動bokeh

bokeh serve filename.py --allow-websocket-origin=127.0.0.1:5000

(正如 bigreddot 提到的,它必須是127.0.0.1 ,而不是localhost

現在flask應該顯示情節。

順便說一句:如果我使用沒有任何 HTML 標簽的模板

render_template_string('''{{ tag|safe }}''', tag=tag) 

那么瀏覽器可能會將所有代碼( <script ...></scrip> )視為<head></head> ,它不會顯示它,因為瀏覽器從不顯示<head></head>元素,即使如果有正確的圖像或情節。


暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM