I want my function to do one thing if it's called from the IPython notebook and another thing if its called from a console or library code. In particular I'm making a progress bar with the desired following behavior:
Notebook: Immediately return a widget
Console: Block and dump information to sys.stdout
Is there a flag somewhere I can check to determine if the user called my function from a notebook or otherwise?
You can't detect that the frontend is a notebook with perfect precision, because an IPython Kernel can have one or more different Jupyter frontends with different capabilities (terminal console, qtconsole, notebook, etc.). You can, however, identify that it is a Kernel and not plain terminal IPython:
import sys
def is_kernel():
if 'IPython' not in sys.modules:
# IPython hasn't been imported, definitely not
return False
from IPython import get_ipython
# check for `kernel` attribute on the IPython instance
return getattr(get_ipython(), 'kernel', None) is not None
Since the notebook is so much more popular than other frontends, this indicates that the frontend is probably a notebook, but not conclusively. So you want to be sure there's a way out for the more primitive frontends.
Related
I am fairly new to python and using VScode. I have been trying to work on a fairly simple project and I've been stuck at this problem for quite some time now.
I have created a venv in which I have assigned the correct venv interpreter path within VScode. All I am trying to do is use binance's websocket to retrieve live data at 1 minute intervals.
I have two problems, in which one may be causing the other. The "ws" as my input shows a warning that is not being accessed by Pylance. When I run my program on VScode, nothing happens... all I see in the terminal was that my code executed but I do not see any data being retrieved.
What's weird is that if I run the same code in Google Collaboration the code executes perfectly... this leads me to believe I have to have something wrong within my VScode settings. I have followed the Binance Documentation but still no luck. Please help, I've been stuck on this for days and cannot find the answer. Any help will be appreciated greatly!!
import websocket
socket = 'wss://stream.binance.com:9443/ws/btcusdt#kline_1m'
def on_open(ws):
print('Connection Open')
def on_close(ws):
print('Connection Closed')
def on_message(ws, message):
print(message)
ws = websocket.WebSocketApp(socket,on_open=on_open, on_close=on_close, on_message=on_message)
ws.run_forever()
The problem occurs when the module is installed in another environment and it is not selected.
to solve it you have to change the execution environment as indicated in the images.
or else in vscode View => command Pallete =>python select interpreter
It worked for me, I hope it works for you
I am trying to create an extension for jupyter-lab that interacts with a running ipython kernel, running either in a console or a notebook.
As a minimal example I am trying to get two "hello world" type programs. The first should print a message to stdout of the running kernel that is input in the extension. The second should log a message to the console of the extension that is input in the running ipython kernel.
To understand jupyter-lab extension I started with the astronomy picture of the day! tutorial. Although this covers the basics of extensions this does not address interacting with a running kernel.
From "really confused with jupyter notebook lab extensions and ipywidgets"! it seems I should be looking at the Comms module however this is documentation for Jupyter-notebook and not for Jupyter-lab. As far as I understand, Jupyter-lab is sufficiently different under the hood that this should not work.
Could someone explain to me how to create an extension that interacts with a running kernel and what the appropriate documentation/tutorial is to look at if it exists?
I'm currently running Robotframework in Eclipse on Windows 10 OS. I'm using an external python library that allows students and teachers to use this extracted library to connect to our hardware devices. I'm automating the extractions from the main site package made by our developers. If more than one device is plugged into the USB ports on the PC, then the code does the following:
x = input("Select one device:")
selected = int(x)
This causes a terminal prompt so the user has to type in a 0, or 1 for example, then hit the ENTER key. User response will allow the code to further process a connect to the selected device. Note, this prompt is not a GUI. So when I run Robotframework, it will execute the steps up to the point where it's prompting.
It seems like this should be pretty easy, but I can't seem to figure it out. Since you're inside a piece of code that's waiting for input, how do you make RobotFramework do something with it?
Edit: It occurs to me that maybe there's a way to execute a delayed Robotframework step that starts an external python command after a specified time, to throw a '0' and a RETURN key response. I had a python file made from an import of pynput.py library which appears to work from the command line execution (prints a 0, or a 1, and a return line feed). There's gotta be an easier way I'd think, but I don't know what it is.
Edit: Can I run a keyword from a listener that watches for the command prompt and the keyword runs another python file to feed the prompt? If I get this to work, then all I have to do is leave the devices on the USB port (or hub for that matter), and select the devices I want to do further testing on. Our devices are supported Blooth tooth as well but I need to run both USB and BLE tests to verify our Python extractions the teachers and students can use.
Edit: The other option is to use a software programmable hub and select the USB with a specific device on it, but I'm trying to avoid that.
OK, I solved it using Robotframework background process. I wrote a small python file that gets executed from the process. It has a 5 second timer (more than I need) and then Robotframework runs the next Test Case step. The Python file then does some keyboard presses, selecting the port and an ENTER key which goes out to the console (feeding the input prompt). It connects the sensor.
So in my Robotframework Test Case I do the following:
*** Test Case ***
smoke_test
Start process . Python . usbportselect
open usb
The Python program called from the process looks like this:
import time
import pynput
from pynput.keyboard import Key, Controller
keyboard = Controller()
def choose_usb(portvalue)
keyboard.press(portvalue)
keyboard.release(portvalue)
keyboard.press(Key.enter)
keyboard.release(Key.enter)
time.sleep(5)
choose_usb('0')
Note: I'm pretty sure this won't fix all the problems with using processes, but it's at least a start and a way to feed input to a prompt resulted from a future Test Case step
I am trying to figure out a way to capture logs rendered through users interaction with the kernel in Jupyter Lab.
After you jupyter lab --generate-config there will be configuration file with a few logging-related options available:
c.Application.log_datefmt = '%Y-%m-%d %H:%M:%S'
c.Application.log_format = '[%(name)s]%(highlevel)s %(message)s'
c.Application.log_level = 30
With these, it seems to generate some verbose output in the terminal but can someone point me in direction to help understand the following:
How can I get this to log into a file?
This configuration seems to be generating logging handlers which could be altered in the notebook code easily - is there a way to get a lower end logger that can not be altered in user code?
Is this a right way to capture kernel logs (I am looking for something that would capture all the inputs and maybe even outputs sent kernel way)?
I learned this trick from Using IPython console along side IPython notebook . But when I connect this kernel using console, the %load magic will load file into a pager (like you you do 'man thecodefile.py' in console), not to the input line.
Anyone know how to change these behavior back to default?
You ask two question here, please ask them separately.
As for !su ausername there is no way to forward stdin through subprocess.