Tag Archives: Python

How to save HTML to image

As I discussed in How to embed matplotlib figure in html, sometime the return from different third-party tools are HTML. In order to merge different analysis into one, I use image as a media.

How to covert html to image?

  1. Install wkhtmltopdf,
  2. Install python package, imgkit
sudo apt-get install wkhtmltopdf
pip install imgkit

Then you can save HTML to image

import imgkit

imgkit.from_url('http://google.com', 'out.jpg')
imgkit.from_file('test.html', 'out.jpg')

Here is an example to show merged image of backtrader figure report and quantstats html report

How to embed matplotlib figure in html

Sometime, when we use third-party tool, which may provide visualization using matplotlib to plot figure and visualize the results. It is ok if we only use this tool. However, if we want to integrate these figures with other tools. The figures cannot be used. For example, I use backtrader to do backtest and visualize the signal and buy/sell datetime (figure plot), while using quantstats to do capitabl & gain/loss analysis (can view in html). It is better to merge them into one html to do visulization.

Because image is easily embedded in html using base64 coding, I convert plot figure into image base64. The following code snippet do the coversion

def figure2base64(fg):
    img = None
    with BytesIO() as img_buf:
        plt.savefig(img_buf, format='jpg')
        img = base64.b64encode(img_buf.getbuffer()).decode('utf-8')
    return img

The input is a figure instance, which can be plot.show() to display, and the return is base64 encoded image, which can be embedded into html using the following

<img src="data:image/jpeg;base64,base64_image />

Issue of module not found in Python 3.10

After upgrading python from Python 3.8 to 3.10, modules look installed in virtual environment previously, but cannot import. It will report e.g. module not found numpy, module not found pandas

Try uninstall install version and install again like

pip install numpy
pip install XXXX

Still same error when e.g. import numpy


pip uninstall numpy
python -m pip install --user --upgrade numpy

Bokeh – A Python package to create figure & UI

To plot figure in Python, matplotlib is an native module. Now there is another good option, Bokeh, https://docs.bokeh.org/en/latest/index.html. Bokeh is Python lib, which core module is written by JS.

Using Bokeh, you can create more interactive & beautiful user interface using build-in models, or even using native html script (I like). This is a snapshot I use Bokeh to display candlestick using selected date range, and also update information in a table (integrating native html script).

Candlestick plot using Bokeh

How to setup PostgreSQL in Python

  • Install Psycopg2 package in Python
pip install psycopg2 or
pipenv install psycopg2 (virtual python environment)
  • Install postgresql in Ubuntu
sudo apt update
sudo apt install postgresql postgresql-contrib
  • After successfully install postgresql, the default role is postgres. So when you connect postgresql service in your own Ubuntu user, you cannot connect it, reporting the username not in the role. Thus create a role using your own username, and add your username in ROLE of postgres
psql postgres 

postgres=# CREATE ROLE ubuntu_username superuser;
postgres=# ALTER ROLE ubuntu_username WITH LOGIN;
  • In python, connect postgresql, e.g.
import psycopg2

conn = psycopg2.connect("dbname=test user=user_name")
« Older Entries