-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatplotlib_backend.py
More file actions
34 lines (28 loc) · 1.11 KB
/
matplotlib_backend.py
File metadata and controls
34 lines (28 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import threading
import os
import telegram
from matplotlib.backend_bases import Gcf
from matplotlib.backends.backend_agg import FigureCanvasAgg
import mainip
FigureCanvas = FigureCanvasAgg
manager, myChatID, bot = None, None, None # those objects are initialized by mainip
def show(*args, **kwargs):
for num, figmanager in enumerate(Gcf.get_all_fig_managers()):
pid = threading.get_ident()
try:
consoleID = manager.threads[pid].chatID
except KeyError:
consoleID = myChatID
filename = '%s.png' % consoleID
figmanager.canvas.figure.savefig(filename)
sendPhoto(consoleID, filename)
os.remove(filename)
def sendPhoto(chatID, filename):
console = manager.data[chatID]
console.history.index = 0
try:
bot.edit_message_reply_markup(chat_id=console.lastMessage.chat_id, message_id=console.lastMessage.message_id)
except (telegram.error.BadRequest, AttributeError):
pass
reply_markup = console.history()
console.lastMessage = bot.send_photo(chat_id=chatID, photo=open(filename, 'rb'),reply_markup=reply_markup)