Skip to main content
 首页 » 编程设计

python之如何让我的 Bokeh 图出现在我的 Django 应用程序中

2024年09月03日27xing901022

我一直在使用 jupyter notebook 使用我的 csv 文件进行一些可视化。我需要能够在我的 Django 网络应用程序上显示这张 map ,但不知道如何去做。

我已经尝试将代码放入我的 View 文件并将其呈现到我的 html 文件,但我不知道如何在这种情况下使用我的 csv 文件。

import bokeh  
import pandas as pd 
from bokeh.io import output_notebook, show 
from bokeh.plotting import figure 
from bokeh.models import ColumnDataSource, HoverTool 
from bokeh.transform import linear_cmap 
from bokeh.palettes import Spectral6, RdYlGn, viridis, Plasma256, RdYlGn10, YlOrRd4, Reds9 
from bokeh.tile_providers import CARTODBPOSITRON_RETINA 
from pygeotile.point import Point 
output_notebook() 
%matplotlib inline 
 
pred = r'path' 
pred = pd.read_csv(pred) 
 
for index, row in pred.iterrows(): 
        point = Point.from_latitude_longitude(latitude=row['Latitude'], longitude=row['Longitude']) 
        pred.at[index,'x'] = point.meters[0] 
        pred.at[index,'y'] = point.meters[1] 
        pred.at[index,'size'] = 15 # int(row[bnf]/100) 
 
p = figure(plot_width=900, plot_height=400,  
           x_axis_type="mercator",  
           y_axis_type="mercator", 
           x_range=(-928267, -573633),  
           y_range=(7168390, 7422161)) 
p.add_tile(CARTODBPOSITRON_RETINA) 
 
mapper = linear_cmap(field_name='type', palette=Spectral6,low=0 ,high=1) 
 
source = ColumnDataSource(pred_crime) 
p.circle(x='x', y='y', source=source, size='size', fill_color=mapper, line_alpha=0.5, line_color='black') 
p.add_tools(HoverTool(tooltips=[("type","Type")])) 
show(p) 

我想知道将这些文件放在哪里以及我需要更改哪些内容才能使其在 pycharm 中运行

请您参考如下方法:

这应该适合您。将 index.html 放入 templates 目录。

index.html

<!DOCTYPE html> 
<html lang='en'> 
    <head> 
        {{ resources | safe }} 
        <title>testing bokeh...</title> 
    </head>   
    <body> 
        {{ div | safe }} 
        {{ script | safe }} 
    </body> 
</html> 

django_app.py

import pandas as pd 
from bokeh.plotting import figure 
from bokeh.models import ColumnDataSource, HoverTool 
from bokeh.transform import linear_cmap 
from bokeh.palettes import Spectral6, RdYlGn, viridis, Plasma256, RdYlGn10, YlOrRd4, Reds9 
from bokeh.tile_providers import CARTODBPOSITRON_RETINA 
from pygeotile.point import Point 
from bokeh.embed import components 
from bokeh.resources import INLINE 
from django.shortcuts import render_to_response 
 
#output_notebook() 
#%matplotlib inline 
 
pred = r'path' 
pred = pd.read_csv(pred) 
 
for index, row in pred.iterrows(): 
        point = Point.from_latitude_longitude(latitude=row['Latitude'], longitude=row['Longitude']) 
        pred.at[index,'x'] = point.meters[0] 
        pred.at[index,'y'] = point.meters[1] 
        pred.at[index,'size'] = 15 # int(row[bnf]/100) 
 
p = figure(plot_width=900, plot_height=400,  
           x_axis_type="mercator",  
           y_axis_type="mercator", 
           x_range=(-928267, -573633),  
           y_range=(7168390, 7422161)) 
p.add_tile(CARTODBPOSITRON_RETINA) 
 
mapper = linear_cmap(field_name='type', palette=Spectral6,low=0 ,high=1) 
 
source = ColumnDataSource(pred_crime) 
p.circle(x='x', y='y', source=source, size='size', fill_color=mapper, line_alpha=0.5, line_color='black') 
p.add_tools(HoverTool(tooltips=[("type","Type")])) 
script, div = components(p) 
return render_to_response('index.html', {'resources' = INLINE.render(), 'script': script, 'div': div})