pythoncalendarview

Building dynamic calendar using calendar-view is failing


I am trying to build a calendar image using the package calendar-view. Below is the code from calendar-view:

from calendar_view.calendar import Calendar
from calendar_view.core import data
from calendar_view.core.event import Event

config = data.CalendarConfig(
    lang='en',
    title='Sprint 23',
    dates='2019-09-23 - 2019-09-27',
    show_year=True,
    mode='working_hours',
    legend=False,
)
events = [
    Event('Planning', day='2019-09-23', start='11:00', end='13:00'),
    Event('Demo', day='2019-09-27', start='15:00', end='16:00'),
    Event('Retrospective', day='2019-09-27', start='17:00', end='18:00'),
]

data.validate_config(config)
data.validate_events(events, config)

calendar = Calendar.build(config)
calendar.add_events(events)
calendar.save("sprint_23.png")

This code works perfectly fine, however, I have been trying to build a similar calendar dynamically. As in, number events may increase or decrease. Is there a way to make this code behave more dynamic?

Here is some info on how I have tried making this dynamic: I wrote the event details (shown below) in to a txt file.

[
    Event('Planning', day='2019-09-23', start='11:00', end='13:00'),
    Event('Demo', day='2019-09-27', start='15:00', end='16:00'),
    Event('Retrospective', day='2019-09-27', start='17:00', end='18:00'),
]

Then read the text file in to the field 'event' (as shown below)

event = open("instruction.txt", "r")

But the code fails because it considers everything that has been read from file as a 'str'. Please see error below:

AttributeError: 'str' object has no attribute 'get_start_date'

Solution

  • You can use this code:

    with open('instruction.txt', 'r') as file:
        file_content = file.read()
    events = eval(file_content)
    

    eval() is the function that executes the string and returns the Python object.