Odoo con bokeh
Veremos como sacar un gráfco de bokeh en Odoo 12
En una instalación estandar de Odoo 12 hay que instalar:
pip3 install bokeh
pip3 install selenium
sudo apt-get install firefox-geckodriver
Creamos una carpeta llamada wizards y dentro creamos un fichero llamado por ejemplo bokeh_data.py con el siguiente código:
# -*- coding: utf-8 -*-
from odoo import models, fields, api, _
from bokeh.plotting import figure, output_file, save
from bokeh.io.export import get_screenshot_as_png
from bokeh.io import export_png
from odoo.modules.module import get_module_resource
from odoo import tools
import base64
class BokehData(models.TransientModel):
_name = 'bokeh.data'
_description = 'Bokeh Data'
image = fields.Binary(string='Image',default=lambda s: s._default_product_image(), help='My Image')
@api.model
def _default_product_image(self):
x = [1, 2, 3, 4, 5]
y = [6, 7, 2, 4, 5]
p = figure(title="simple line example", x_axis_label='x', y_axis_label='y')
# add a line renderer with legend and line thickness
p.line(x, y, legend_label="Temp.", line_width=2)
export_png(p, filename="/opt/odoo12/odoo/addons/bokeh_data/test/plot.png")
image_path = get_module_resource(
'bokeh_data', 'test', 'plot.png'
)
with open(image_path, 'rb') as handler:
image_data = handler.read()
return tools.image_resize_image_big(
base64.b64encode(image_data)
)
Ves que hay una ruta fija... que tendrias que adaptar a tu instalación. El módulo en este caso se ha llamado bokeh_data y se ha creado una carpeta llamada test para dejar las fotos.
En la misma carpeta wizard creamos un fichero XML con el nombre bokeh_data_view.xml y el contenido:
<?xml version="1.0" encoding="utf-8"?> <odoo> <record id="view_clean_data_form" model="ir.ui.view"> <field name="name">bokeh.data.wiz.form</field> <field name="model">bokeh.data</field> <field name="arch" type="xml"> <form string="Bokeh Data"> <group> <group> <field name="image" widget="image"/> </group> </group> </form> </field> </record> <record id="action_clean_data_wizard" model="ir.actions.act_window"> <field name="name">Bokeh Data</field> <field name="res_model">bokeh.data</field> <field name="view_type">form</field> <field name="view_mode">form</field> <field name="target">new</field> </record> <menuitem id="clean_data_menu" name="Bokeh Data" string="Bokeh Data" parent="base.menu_administration" sequence="0"/> <menuitem id="clean_data_sub_menu" parent="clean_data_menu" action="action_clean_data_wizard" name="Clean Data" string="Clean Data" sequence="0"/> </odoo>
Cuando lo instales, verás que en ajustes sale una nueva opción de menú que te muestra el gráfico.