Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
thp committed Aug 7, 2013
0 parents commit 34ea420
Show file tree
Hide file tree
Showing 30 changed files with 1,790 additions and 0 deletions.
14 changes: 14 additions & 0 deletions COPYING
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
PyOtherSide: Asynchronous Python 3 Bindings for Qt 5
Copyright (c) 2011, 2013, Thomas Perl <m@thp.io>

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
38 changes: 38 additions & 0 deletions README
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
PyOtherSide: Asynchronous Python 3 Bindings for Qt 5
====================================================

This is a QML Plugin that provides access to a Python 3 interpreter from QML.
See examples/ for some basic code examples what you can do with this.

Requirements:

Qt >= 5.0.2
Python >= 3.3.0


To build and install the QML plugin:

qmake
make
make install


To run the included unit tests:

qmake
make
tests/tests


To run one of the included examples (after installing the plugin):

qmlscene examples/simple/simple.qml
qmlscene examples/events/main.qml
qmlscene examples/atexit/main.qml
qmlscene examples/notes/main.qml


Website and Git repository:

http://thp.io/2011/pyotherside/
http://github.com/thp/pyotherside/
2 changes: 2 additions & 0 deletions examples/atexit/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
This examples shows how to do cleanups and other things when
the application exists by using pyside.atexit().
8 changes: 8 additions & 0 deletions examples/atexit/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import pyotherside

def called_when_exiting():
print('Now exiting the application...')

pyotherside.atexit(called_when_exiting)
print('python loaded')

19 changes: 19 additions & 0 deletions examples/atexit/main.qml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

import QtQuick 2.0
import io.thp.pyotherside 1.0

Rectangle {
width: 200
height: 200
color: 'red'

Python {
id: python
Component.onCompleted: {
addImportPath('examples/atexit');
importModule_sync('main');
}

onError: console.log('error in python: ' + traceback);
}
}
1 change: 1 addition & 0 deletions examples/events/README
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This example demonstrates the use of pyotherside.send() to send events to Qt.
21 changes: 21 additions & 0 deletions examples/events/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

import pyotherside

import threading
import time

COLORS = ['red', 'green', 'blue']

def thread_func():
i = 0
while True:
pyotherside.send('append', 'Next Number: ', i)
if i % 2 == 0:
color = COLORS[int((i / 2) % len(COLORS))]
pyotherside.send('color', color)
i += 1
time.sleep(1)

thread = threading.Thread(target=thread_func)
thread.start()

37 changes: 37 additions & 0 deletions examples/events/main.qml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

import QtQuick 2.0
import io.thp.pyotherside 1.0

Rectangle {
width: 400
height: width

Python {
id: py

Component.onCompleted: {
// Add the Python library directory to the import path
addImportPath('examples/events');

// Import the main module
importModule('main', function () {
console.log('Main module is now imported');
});
}

onReceived: {
var command = data[0], args = data.splice(1);

if (command == 'append') {
output.text += '\n' + args;
} else if (command == 'color') {
output.color = args[0];
}
}
}

Text {
id: output
anchors.centerIn: parent
}
}
2 changes: 2 additions & 0 deletions examples/notes/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
A very simple notetaking application that uses Python to load
and save a string in a text file in the user's home directory.
39 changes: 39 additions & 0 deletions examples/notes/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

import os
import threading
import time

import pyotherside

class Notes:
def __init__(self):
self.filename = os.path.expanduser('~/pyotherside_notes.txt')
self.thread = None
self.new_text = self.get_contents()

def save_now(self):
print('Saving file right away at exit')
self._update_file(now=True)

def _update_file(self, now=False):
if not now:
time.sleep(3)
print('Saving file now')
open(self.filename, 'w').write(self.new_text)
self.thread = None

def get_contents(self):
if os.path.exists(self.filename):
return open(self.filename).read()
else:
return '<new file>'

def set_contents(self, text):
self.new_text = text
if self.thread is None:
print('Scheduling saving of file')
self.thread = threading.Thread(target=self._update_file)
self.thread.start()

notes = Notes()
pyotherside.atexit(notes.save_now)
35 changes: 35 additions & 0 deletions examples/notes/main.qml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

import QtQuick 2.0
import io.thp.pyotherside 1.0

Rectangle {
width: 300
height: 200

TextInput {
id: ti

anchors.fill: parent

onTextChanged: {
py.call('main.notes.set_contents', [text], function() {
console.log('Changes sent to Python');
});
}

Python {
id: py
Component.onCompleted: {
addImportPath('examples/notes');
importModule('main', function () {
console.log('imported python module');
call('main.notes.get_contents', [], function(result) {
console.log('got contents from Python: ' + result);
ti.text = result;
});
});
}
}
}

}
1 change: 1 addition & 0 deletions examples/simple/README
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Just some simple API usage examples for PyOtherSide.
54 changes: 54 additions & 0 deletions examples/simple/simple.qml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import QtQuick 2.0
import io.thp.pyotherside 1.0

Rectangle {
width: 200
height: 200
color: 'blue'

ListView {
id: listView
anchors.fill: parent
delegate: Text { color: 'white'; text: modelData }
}

Python {
id: python

Component.onCompleted: {
// Asynchronous module importing
importModule('os', function() {
console.log('Python module "os" is now imported');

// Asynchronous function calls
call('os.listdir', [], function(result) {
console.log('dir listing: ' + result);
listView.model = result;
});

// Synchronous calls - avoid these, they block the UI
call_sync('os.chdir', ['/']);
console.log('files in /: ' + call_sync('os.listdir', ['.']));
});

// sychronous imports and calls - again, avoid!
importModule_sync('pyotherside');
call_sync('pyotherside.send', ['hello world!', 123]);

// error handling
importModule_sync('thismoduledoesnotexisthopefully');
evaluate('[ 123 [.syntax234-error!');
}

onError: {
// when an exception is raised, this error handler will be called
console.log('python error: ' + traceback);
}

onReceived: {
// asychronous messages from Python arrive here
// in Python, this can be accomplished via pyotherside.send()
console.log('got message from python: ' + data);
}
}
}
3 changes: 3 additions & 0 deletions pyotherside.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
TEMPLATE = subdirs
SUBDIRS += src tests
CONFIG += ordered
4 changes: 4 additions & 0 deletions python.pri
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Link against Python 3.3
PYTHON_CONFIG = python3.3-config
QMAKE_LIBS += $$system($$PYTHON_CONFIG --ldflags)
QMAKE_CXXFLAGS += $$system($$PYTHON_CONFIG --includes)
Loading

0 comments on commit 34ea420

Please sign in to comment.