Skip to main content

implement for Platform Channels for python

Project description

flutter_channel

With this package developers can use python in there flutter applications on windows.

how does it work?

This package depends on unix sockets (TCP) protocol.

how to use?

1. create host

You have to create instance from Host class to use it in channels binding as following:

    from flutter_channel.host import Host
    host=Host()

2. create channels and bind it

There is number of built in channel types to use like: BytesChannel,JsonChannel, StringChannel and MethodChannel.

from flutter_channel.channels import BytesChannel,JsonChannel, StringChannel,MethodChannel
channel1=BytesChannel('channel1')
channel2=StringChannel('channel2')
channel3=JsonChannel('channel3')
channel4=MethodChannel('channel4')
host.bindChannel(channel1)
host.bindChannel(channel2)
host.bindChannel(channel3)
host.bindChannel(channel4)

3. set channel handler

The handler of the channel is a function that receive the messages that is sent to the channel, Each handler should take tow parameters message and reply.

message is the first parameter and it is the massage that was received and it's type depends on the channel type see the following table.

Channel Type Channel Output
BytesChannel bytes
StringChannel str
JsonChannel dict or list
MethodChannel depends on reply that comes from dart can be any primitive type like int ,str,bool or list
custom channel depends on the implement of the encodeOutput and decodeOutput methods

reply is the second parameter, it is instance of Reply you should use this object to send reply on the received message, You can reply with another message or reply with None. Your reply will not be sent to the channel handler in the dart side. It will be send to the send Future that sent the original message.

You have to send reply otherwise dart Future will not complete

example 1

def handler(msg,reply):
    # do some logic
    reply.reply(None)
channel.setHandler(handler)

example 2

    def handler(msg,reply):
    if msg.method=='add':
        reply.reply(add(msg.args[0],msg.args[1],))
    if msg.method=='sub':
        reply.reply(sub(msg.args[0],msg.args[1],))

    if msg.method=='mul':
        reply.reply(mul(msg.args[0],msg.args[1],))

    if msg.method=='div':
        reply.reply(div(msg.args[0],msg.args[1],))
    else:
        raise PythonChannelMethodException(404,'method not found','method not found')
    methodChannel.setHandler(handler)

send message

You can send message to dart side using send(message,callback) method where message type depends on the channel type see the following table and callback is a function that will be invoked when reply comes back from dart and it take one parameter present the reply message.

Channel Type Channel input
BytesChannel bytes
StringChannel str
JsonChannel dict or list
MethodChannel MethodCall
custom channel depends on the implement of the encodeInput and decodeInput methods

examples

    def callBack(replyMessage):
        pass

    bytesChannel.send(bytes([1,1,4,5]),callBack)
    stringChannel.send('hello world',callBack)
    jsonChannel.send({"hello":"world"},callBack)

    def callBackMethod(replyMessage,exception):
        pass

    methodChannel.send(MethodCall(method='sayHello',args={"name":'ghale'}),callBackMethod)
    # or
    methodChannel.invokeMethod(method='sayHello',args={"name":'ghale'},callback=callBackMethod)

MethodChannel

There is some notes we have to mention to about MethodChannel usage.

1. the handler

The handler of the MethodChannel receive two parameter first one is MethodCall instance and second one is Reply instance.

2. the reply callback

the reply callback function take two parameter, First one is the reply message and the second on is exception with PythonChannelMethodException type if exception raised in dart side otherwise the parameter will be None.

3. raise exception in the handler

You can raise PythonChannelMethodException in the handler this exception will be sent by the channel and will throw it to the send Future in dart side.

example
def handler(msg,reply):
    if msg.method=='add':
        reply.reply(add(msg.args[0],msg.args[1],))
if msg.method=='sub':
    reply.reply(sub(msg.args[0],msg.args[1],))

if msg.method=='mul':
    reply.reply(mul(msg.args[0],msg.args[1],))

if msg.method=='div':
    reply.reply(div(msg.args[0],msg.args[1],))
else:
    raise PythonChannelMethodException(404,'method not found','method not found')

print() function

This package change the behavior of the print() the stdout of the python will be the debug console of the flutter Note: you should't use print before create Host instance

create your own channel type

You can create your own channel by write class that inherit Channel class. You should implement 4 method encodeInput, encodeOutput ,decodeInput and decodeOutput

  • encodeInput convert the input of the channel from bytes
  • encodeOutput convert the output of the channel from bytes
  • decodeInput convert the input of the channel to bytes
  • decodeOutput convert the input of the channel to bytes where the input is what the channel send and the output is what the channel receive

for example

    from flutter_channel.channels import Channel,
    class StringChannel(Channel):
    
    def encodeInput(self,data:bytes)->str: 
        return data.decode('utf-8')
    def decodeInput(self,data:str)->bytes: 
        return data.encode('utf-8')
    def encodeOutput(self,data:bytes)->str: 
        return data.decode('utf-8')
    def decodeOutput(self,data:str)->bytes: 
        return data.encode('utf-8')

release mode

in release mode you have to compile you main python file to an executable file, We recommend you to use PyInstaller. Note: you have to build the executable file with console otherwise the package will not work

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

flutter_channel-0.0.4.tar.gz (10.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

flutter_channel-0.0.4-py3-none-any.whl (12.6 kB view details)

Uploaded Python 3

File details

Details for the file flutter_channel-0.0.4.tar.gz.

File metadata

  • Download URL: flutter_channel-0.0.4.tar.gz
  • Upload date:
  • Size: 10.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.2

File hashes

Hashes for flutter_channel-0.0.4.tar.gz
Algorithm Hash digest
SHA256 2709b0a82d4325fd6da287b359ed97f152e763f9f6d20c56935d21d0bc013c8f
MD5 7cf5ee64b63f18874a83fce566ea349d
BLAKE2b-256 071cb14e88dd258dd92dda124ef15c6928cd02e35f43cd437d20ce6a48f0651c

See more details on using hashes here.

File details

Details for the file flutter_channel-0.0.4-py3-none-any.whl.

File metadata

File hashes

Hashes for flutter_channel-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 9df691973f60f7e7b7359e11635bac048b5c499a5ef0da4b578e2e05e1ec668a
MD5 390e24a537d3d15718a3dda24f1b2720
BLAKE2b-256 ddfe32942eac025a4781f53b8ed5740a9d3e662c8c9011069e753f1c188aa119

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page