Display a created heatmap on my web app

Summary

Iā€™ve got a Google sheets document with the number of pages I read each day by date. Iā€™ve created a heatmap ā€˜calendarā€™ using the ā€˜julyā€™ package and I want to display it on my Streamlit application but I donā€™t know how.

I reviewed the documentation but didnā€™t see anything that seemed relevant. I tried using plost but couldnā€™t get it to work. Any suggestions would be greatly appreciated.

Code snippet:

import streamlit as st
import july
import gspread
from oauth2client.service_account import ServiceAccountCredentials
import pandas as pd

url = 'https://docs.google.com/spreadsheets/'

# Set up API credentials and open the worksheet
scope = ['https://spreadsheets.google.com/feeds',
         'https://www.googleapis.com/auth/drive']
credentials = ServiceAccountCredentials.from_json_keyfile_name(
    'cred.json', scope)
gc = gspread.authorize(credentials)
workbook = gc.open_by_url(url)
worksheet = workbook.worksheet('Sheet1')

data = worksheet.get_all_values()
headers = data.pop(0)

df = pd.DataFrame(data, columns=headers)

df['Dates'] = pd.to_datetime(df.Dates)
df['Dates'] = df['Dates'].dt.strftime('%Y-%m-%d')

july.heatmap(dates=df.Dates,
                       data=df.Pages,
                       cmap='github',
                       month_grid=True,
                       horizontal=True,
                       value_label=True,
                       date_label=False,
                       weekday_label=True,
                       month_label=True,
                       year_label=True,
                       colorbar=True,
                       fontfamily="monospace",
                       fontsize=12,
                       title="Daily Pages Read")

Expected Behavior
I would like it just to display the heatmap as it does in Jupyter notebook.

image

July is based on Matplotlib, so you need to create a matplotlib Figure first, tell July to draw your heatmap in one of the Axes of the figure, and then pass that figure to st.pyplot.

import matplotlib.pyplot as plt

fig, ax = plt.subplots()  ## <- Create matplotlib Figure & Axes

july.heatmap(
    dates=df.Dates,
    data=df.Pages,
    cmap='github',
    month_grid=True,
    horizontal=True,
    value_label=True,
    date_label=False,
    weekday_label=True,
    month_label=True,
    year_label=True,
    colorbar=True,
    fontfamily="monospace",
    fontsize=12,
    title="Daily Pages Read",
    ax=ax   ## <- Tell July to put the heatmap in that Axes
)

st.pyploy(fig)  ## <- Tell Streamlit to show that figure

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.