I would like to use the ConnectionPortObject to pass my BrowserSession onto another node.
I tried many ways to implement my output port but seem to get nowhere.
As of right now I use this code to implement my output port:
def execute(self, exec_context):
match self.browser_selection.driverSelection:
case "Chrome":
driver = chrome_window()
driver.get(self.browser_configuration.urlSelection)
case "Edge":
driver = edge_window()
driver.get(self.browser_configuration.urlSelection)
case "Firefox":
driver = firefox_window()
driver.get(self.browser_configuration.urlSelection)
return knext.ConnectionPortObject
I get the following error message: “Execute failed: type object ‘ConnectionPortObject’ has no attribute ‘id’”
Although i tried to add an ID, I get the same message.
I know I have to pass my “driver” - data, but I cannot find a solution.
Different AI suggested to implement a class like:
class BrowserSessionObject(ConnectionPortObjects)...
There isn’t a complete example available for connection port objects, but you can define them similarly to other custom port objects. Extend the ConnectionPortObject class instead of PortObject in your implementation and override the necessary methods.
Thank you @bwilhelm for sharing!
It helped me setting up my output-port for my first Node. However, using the same code, I have trouble setting up my input port.
Please find my code below:
The error message says that the type of the input port of the node where you showed the code does not match the type of the output port of your “first” node. What does the @knext.output_port line look like for your first node?
my output_port is created the same way as in my 2nd node. So it would be the following:
... (same as in second node -> see above)
myPortType = knext.port_type(name="Browser Session", object_class=BrowserSessionObject, spec_class=MyPortObjectSpec)
@knext.output_port(name="Browser Session", description="The output of your current browser session.", port_type=myPortType)
That’s interesting. Are you registering the myPortType twice? This should only be possible once. You need to use the same registered port type for all ports that shall work with your Browser Session.
Hi, I have the feeling that we running a bit in circles.
Do you have an ad-hoc concrete example on how we hand over this kind of port type?`
Thank you in advance
Create a Custom Connection Object: You need to define a custom class that inherits from ConnectionPortObject. This class should store your driver instance and implement any required methods, including the get_id() method.
Modify the execute Method: In your execute method, you need to return an instance of your custom BrowserSessionObject class with the driver as a parameter.
python
Copy code
def execute(self, exec_context):
match self.browser_selection.driverSelection:
case "Chrome":
driver = chrome_window()
driver.get(self.browser_configuration.urlSelection)
case "Edge":
driver = edge_window()
driver.get(self.browser_configuration.urlSelection)
case "Firefox":
driver = firefox_window()
driver.get(self.browser_configuration.urlSelection)
# Return the custom ConnectionPortObject
return BrowserSessionObject(driver)
Ensure Proper Registration: Make sure that your custom BrowserSessionObject is properly registered within KNIME so that the platform recognizes and processes it correctly.
Have not (yet) used Connection Port Objects, but can share what helped me understand better how it all works for custom port objects.
Find a KNIME extension that “does something broadly similar” to what you plan to do and that is python based - for your case the KNIME Web Interaction extension ticks that box: KNIME Web Interaction (Labs) – KNIME Community Hub
Install the extension and then inspect the python code. For the Web Interactions Extension you can find the node implementations in this folder (starting from your KNIME root folder): knime\plugins\org.knime.python.web_5.3.0.v202407021847\src\main\python\nodes\ ==> interact.py, extract.py invoke.py
The Port Objects are defined here:knime\plugins\org.knime.python.web_5.3.0.v202407021847\src\main\python\util\common.py
study the set up and the flow by looking at a concrete example…
Sneak peak of the connection objects used by the above mentioned extension from common.py: