I want to use IPython notebook's IPython.display.display from within a thread, as such:
def foo():
display(HTML('<div id="foobar">foobarbaz</div>'))
threading.Thread(target=foo).start()
IPython notebook spits out the following error:
Exception in thread Thread-17:
Traceback (most recent call last):
File "/usr/lib/python3.5/threading.py", line 914, in _bootstrap_inner
self.run()
File "/usr/lib/python3.5/threading.py", line 862, in run
self._target(*self._args, **self._kwargs)
File "<ipython-input-47-921e56bac735>", line 2, in foo
display(HTML('<div id="foobar">foobarbaz</div>'))
File "/usr/local/lib/python3.5/dist-packages/IPython/core/display.py", line 171, in display
publish_display_data(data=format_dict, metadata=md_dict)
File "/usr/local/lib/python3.5/dist-packages/IPython/core/display.py", line 121, in publish_display_data
metadata=metadata,
File "/usr/local/lib/python3.5/dist-packages/ipykernel/zmqshell.py", line 111, in publish
for hook in self.thread_local.hooks:
AttributeError: '_thread._local' object has no attribute 'hooks'
I figure IPython's display is not thread safe? Is there a way to get it to point to the main thread instead of the local thread? Or perhaps a way to get the IPython.display.DisplayHandle? Maybe the thing to do is to have a loop around a queue.Queue on the main thread and display objects as they get added into that queue?
Thank you!
You can use an Output widget to display your HTML. This worked for me:
import ipywidgets as widgets
from IPython.display import display, HTML
import threading
out = widgets.Output()
def foo():
with out:
display(HTML('<div id="foobar">foobarbaz</div>'))
threading.Thread(target=foo).start()
out
Related
I'm using Flet and I want for my app to launch a link when clicking on a button.
According to the docs, I can use launch_url method. But when I tried, I got the following error:
Exception in thread Thread-6 (open_repo):
Traceback (most recent call last):
File "C:\Users\Iqmal\AppData\Local\Programs\Python\Python311\Lib\threading.py", line 1038, in _bootstrap_inner
self.run()
File "C:\Users\Iqmal\AppData\Local\Programs\Python\Python311\Lib\threading.py", line 975, in run
self._target(*self._args, **self._kwargs)
File "d:\Iqmal\Documents\Python Projects\flet-hello\main.py", line 58, in open_repo
ft.page.launch_url('https://github.com/iqfareez/flet-hello')
^^^^^^^^^^^^^^^^^^
AttributeError: 'function' object has no attribute 'launch_url'
Code
import flet as ft
def main(page: ft.Page):
page.padding = ft.Padding(20, 35, 20, 20)
page.theme_mode = ft.ThemeMode.LIGHT
appbar = ft.AppBar(
title=ft.Text(value="Flutter using Flet"),
bgcolor=ft.colors.BLUE,
color=ft.colors.WHITE,
actions=[ft.IconButton(icon=ft.icons.CODE, on_click=open_repo)])
page.controls.append(appbar)
page.update()
def open_repo(e):
ft.page.launch_url('https://github.com/iqfareez/flet-hello')
ft.app(target=main, assets_dir='assets')
I 'solved' this issue by moving the open_repo() function inside the main() function block. Now, the link can be opened on a browser when clicked. Example:
def main(page: ft.Page):
def open_repo(e):
page.launch_url('https://github.com/iqfareez/flet-hello')
appbar = ft.AppBar(
title=ft.Text(value="Flutter using Flet"),
bgcolor=ft.colors.BLUE,
color=ft.colors.WHITE,
actions=[ft.IconButton(icon=ft.icons.CODE, on_click=open_repo)])
# code truncated for clarity
Full code here.
From what I'm seeing here and the errors you're getting it's just possible you might have a problem with the installation of Flet. Try installing running in a virtual environment and see if it changes.
Good Luck
I am making a program that will allow my phone to share its clipboard with my PC. I am using Pyperclip as it appears to be the easiest and most cross-platform solution. However, when I run the code below:
...
elif "incoming_clipboard:" in server_command:
print(server_command)
pyperclip.copy(server_command.replace("incoming_clipboard: ", ""))
...
I get the error:
Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/lib/python3.9/threading.py", line 973, in _bootstrap_inner
self.run()
File "/usr/lib/python3.9/threading.py", line 910, in run
self._target(*self._args, **self._kwargs)
File "/home/chris/Documents/Programming/Synchrony/Desktop/Phone.py", line 30, in do_sync
self.handle_commands(bluetooth_socket)
File "/home/chris/Documents/Programming/Synchrony/Desktop/Phone.py", line 65, in handle_commands
pyperclip.copy(server_command.replace("incoming_clipboard: ", ""))
File "/usr/lib/python3.9/site-packages/pyperclip/__init__.py", line 659, in lazy_load_stub_copy
return copy(text)
File "/usr/lib/python3.9/site-packages/pyperclip/__init__.py", line 158, in copy_gtk
cb = gtk.Clipboard()
File "/usr/lib/python3.9/site-packages/gi/__init__.py", line 69, in __getattr__
raise AttributeError(_static_binding_error)
AttributeError: When using gi.repository you must not import static modules like "gobject". Please change all occurrences of "import gobject" to "from gi.repository import GObject". See: https://bugzilla.gnome.org/show_bug.cgi?id=709183
I know its not due to the string being invalid or anything, because 1) I am explicitly converting it from bytes to a string, and 2) it prints perfectly fine. I don't know if this is relevant, but I am using Linux, so perhaps that is affecting Pyperclip?
Do you guys have any solutions?
https://github.com/dynobo/normcap/issues/100
After going to the Github page for PyperClip, I found a similar issue that someone else had that suggested switching to PyClip. After installing PyClip and implementing it in project, I can confirm that it works perfectly without any issues.
I am running two python jobs scheduled at intervals.
activity_url_collector and storage_data_collector are the .py files in the same directory as of index.py.
Below is index.py:
import schedule
import time
import psycopg2
import threading
import activity_url_collector
import storage_data_collector
def main():
def run_threaded(job_func):
job_thread = threading.Thread(target=job_func)
job_thread.start()
schedule.every(3).minutes.do(run_threaded, activity_url_collector)
schedule.every(3).minutes.do(run_threaded, storage_data_collector)
schedule.run_all()
print('Post-Processing-Application is running')
while True:
schedule.run_pending()
time.sleep(1)
if __name__=="__main__":
main()
Detailed Error:
Traceback (most recent call last):
File "/usr/lib/python3.5/threading.py", line 914, in _bootstrap_inner
self.run()
File "/usr/lib/python3.5/threading.py", line 862, in run
self._target(*self._args, **self._kwargs)
TypeError: 'module' object is not callable
What could be going wrong here? Thanks.
You are trying to run activity_url_collector and storage_data_collector in a thread.
Looking at your import both are modules (Python files) which can be run by the interpreter directly, but they are not "callables" as needed for your case.
You can run functions, methods or any object that implements __call__ in a thread. As a solution you could add a main() function to your modules which does the actual work and use activity_url_collector.main as the target for the thread.
I am running Dask Distributed on Linux CentOS 7, with a Python 3.6.2 installation. My computation seems to be getting fine (I am still improving my code, but I am able to have some results), but I keep getting some python errors apparently linked to tornado module. I am only launching a one node standalone Dask distributed cluster.
Here is the most common example:
Exception in thread Client loop:
Traceback (most recent call last):
File "/usr/local/lib/python3.6/threading.py", line 916, in _bootstrap_inner
self.run()
File "/usr/local/lib/python3.6/threading.py", line 864, in run
self._target(*self._args, **self._kwargs)
File "/usr/local/lib/python3.6/site-packages/tornado/ioloop.py", line 832, in start
self._run_callback(self._callbacks.popleft())
AttributeError: 'NoneType' object has no attribute 'popleft'
And here is another one:
tornado.application - ERROR - Exception in callback <bound method WorkStealing.balance of <distributed.stealing.WorkStealing object at 0x7f752ce6d6a0>>
Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/tornado/ioloop.py", line 1026, in _run
return self.callback()
File "/usr/local/lib/python3.6/site-packages/distributed/stealing.py", line 248, in balance
sat = s.rprocessing[key]
KeyError: 'read-block-9024000000-e3fefd2110094168cc0505db69b326e0'
Do you have any idea why? Should I close some connections or stop the standalone cluster?
Yes, if you don't close down the Tornado IOLoop before exiting the process then it can die in an unpleasant way. Fortunately this shouldn't affect your application, except by looking unpleasant.
You might submit a bug report about this, it's still something that we should fix.
I am trying to learn how to use threads with python. this is the code I have been studying:
import time
from threading import Thread
def myfunc(i):
print "sleeping 5 sec from thread %d" % i
time.sleep(5)
print "finished sleeping from thread %d" % i
for i in range(10):
t = Thread(target=myfunc, args=(i,))
t.start()
the program runs fine in command prompt but when I try to run it in idle I get errors like this:
Traceback (most recent call last):
File "C:\Python24\lib\lib-tk\Tkinter.py", line 1345, in __call__
return self.func(*args)
File "C:\Python24\lib\idlelib\ScriptBinding.py", line 165, in run_module_event
interp.runcode(code)
File "C:\Python24\lib\idlelib\PyShell.py", line 726, in runcode
self.tkconsole.endexecuting()
File "C:\Python24\lib\idlelib\PyShell.py", line 901, in endexecuting
self.showprompt()
File "C:\Python24\lib\idlelib\PyShell.py", line 1163, in showprompt
self.resetoutput()
File "C:\Python24\lib\idlelib\PyShell.py", line 1178, in resetoutput
self.text.insert("end-1c", "\n")
File "C:\Python24\lib\idlelib\Percolator.py", line 25, in insert
self.top.insert(index, chars, tags)
File "C:\Python24\lib\idlelib\PyShell.py", line 315, in insert
UndoDelegator.insert(self, index, chars, tags)
File "C:\Python24\lib\idlelib\UndoDelegator.py", line 81, in insert
self.addcmd(InsertCommand(index, chars, tags))
File "C:\Python24\lib\idlelib\UndoDelegator.py", line 116, in addcmd
cmd.do(self.delegate)
File "C:\Python24\lib\idlelib\UndoDelegator.py", line 216, in do
if text.compare(self.index1, ">", "end-1c"):
File "C:\Python24\lib\lib-tk\Tkinter.py", line 2784, in compare
return self.tk.getboolean(self.tk.call(
TclError: expected boolean value but got ""
Is python threading just not stable or am I doing something grossly wrong? The example came from : http://www.saltycrane.com/blog/2008/09/simplistic-python-thread-example/
It sounds like a bug in IDLE, not a problem with Python. The error is coming from Tkinter, which is a Python GUI toolkit, and which IDLE probably uses. I would report it to whoever maintains IDLE.
Not everything runs properly under IDLE. This is because IDLE is a Python program in itself and has its own attributes and state that can sometimes get messed up by your own code. You can tell this is a problem with IDLE because you can see idlelib in the call stack. Also, you're not using TCL/TK at all in your application, but IDLE is, and the call stack shows that too.
I would advise switching to a more 'inert' text editor for working with Python code!