File: ipy.py

package info (click to toggle)
kuttypy 2.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 37,896 kB
  • sloc: python: 58,651; javascript: 14,686; xml: 5,767; ansic: 2,716; makefile: 453; asm: 254; sh: 48
file content (128 lines) | stat: -rw-r--r-- 3,778 bytes parent folder | download
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import os

from IPython.lib import guisupport

from .Qt import QtGui,QtCore,QtWidgets
from .templates import ui_ipy as ipy_template
import sys,string
import time

from qtconsole.rich_jupyter_widget import RichJupyterWidget
from qtconsole.inprocess import QtInProcessKernelManager

# The ID of an installed kernel, e.g. 'bash' or 'ir'.
USE_KERNEL = 'python3'

class myConsole(RichJupyterWidget):
	def __init__(self,customBanner=None):
		"""Start a kernel, connect to it, and create a RichJupyterWidget to use it
		"""
		super(myConsole, self).__init__()
		if customBanner is not None:
			self.banner=customBanner
		self.kernel_manager = QtInProcessKernelManager(kernel_name=USE_KERNEL)
		#print(self.kernel_manager.get_connection_info())
		#self.kernel_manager.set_trait('ip', '0.0.0.0')
		#self.kernel_manager.set_trait('shell_port', 8888)
		#self.kernel_manager.set_trait('control_port', 8889)
		#self.kernel_manager.set_trait('iopub_port', 8890)
		#self.kernel_manager.set_trait('stdin_port', 8891)
		#print(self.kernel_manager.get_connection_info())

		self.kernel_manager.start_kernel()
		self.kernel = self.kernel_manager.kernel

		self.kernel_manager.kernel.gui = 'qt'
		self.font_size = 6

		self.kernel_client = self.kernel_manager.client()
		self.kernel_client.start_channels()

		def stop():
			self.kernel_client.stop_channels()
			self.kernel_manager.shutdown_kernel()
			#guisupport.get_app_qt().exit()

		self.exit_requested.connect(stop)

	def pushVariables(self,variableDict):
		""" Given a dictionary containing name / value pairs, push those variables to the IPython console widget """
		self.kernel.shell.push(variableDict)
	def clearTerminal(self):
		""" Clears the terminal """
		self._control.clear()    

	def printText(self,text):
		""" Prints some plain text to the console """
		self._append_plain_text(text)

	def executeCommand(self,command,hidden=False):
		""" Execute a command in the frame of the console widget """
		self._execute(command,hidden)



class AppWindow(QtWidgets.QMainWindow, ipy_template.Ui_MainWindow):
	def __init__(self, parent=None,**kwargs):
		super(AppWindow, self).__init__(parent)
		self.setupUi(self)
		self.kp=kwargs.get('kp',None)

		self.setWindowTitle('iPython Console : Try out stuff!')
		self.msg = QtWidgets.QLabel()
		self.statusbar.addWidget(self.msg)
		self.msg.setText('Hi!')
		self.timer = QtCore.QTimer()

		try:
			#--------instantiate the iPython class-------
			self.ipyConsole = myConsole("################ Interactive KuttyPy Console ###########\nAccess hardware using the Instance 'kp'.  e.g.  kp.setReg('PORTB',8)\n\n")
			self.layout.addWidget(self.ipyConsole)
		except Exception as e:
			print ("failed to launch iPython. Is it installed?", e)
			self.close()
			return
			

		cmdDict = {}
		#cmdDict = {"analytics":self.analytics}
		if self.kp :
			cmdDict["kp"]=self.kp
		self.ipyConsole.pushVariables(cmdDict)
		self.console_enabled=True

	def importNumpy(self):
		self.ipyConsole.executeCommand('import numpy as np',True)
		self.message('imported Numpy as np')

	def importScipy(self):
		self.ipyConsole.executeCommand('import scipy',True)
		self.message('imported scipy')

	def importPylab(self):
		self.msg.setText('importing Pylab...')
		self.ipyConsole.executeCommand('from pylab import *',True)
		self.message('from pylab import * .  You can use plot() and show() commands now.')

	def message(self,txt):
		self.msg.setText(txt)
		self.timer.stop()
		self.timer.singleShot(4000,self.msg.clear)

	def updateDevice(self,kp):
		self.kp = kp

	def closeEvent(self, event):
		self.timer.stop()
		self.finished=True

	def __del__(self):
		print ('bye')
        		
if __name__ == "__main__":
	app = QtWidgets.QApplication(sys.argv)
	myapp = AppWindow(kp=None)
	myapp.show()
	sys.exit(app.exec_())