Kernel gets busy when using nltk.download - python-3.x

I am using jupyter notebook to practice this problem on kaggle https://www.kaggle.com/c/word2vec-nlp-tutorial/details/part-1-for-beginners-bag-of-words.
When I use the following code
import nltk
nltk.download() # Download text data sets, including stop words
Kernel goes in the busy state and then I am unable to execute any cells further.

When you run nltk.download(), it launches an interactive GUI window that you can use to download resources. But very often this window is hidden behind other windows on your screen. Look for it, download anything you need and then close the downloader window in order for your script to return control to the notebook kernel.
To avoid hanging when your code gets to a download command, you could use a non-interactive download command instead. E.g., nltk.download("brown") for the Brown corpus, or nltk.download("book") to get all resources needed when reading through the nltk book. These carry out the download (even if you already have the requested resource) without opening a GUI window. For this you'll need to know, or guess, the internal name of the resource you want.

Related

Python file immediately closes

As an assignment for one of my classes, the professor had us download a file containing python code as well as a couple of text files that the python code will analyze.
The problem is, when I click to open the python file, the command window opens, then immediately closes. I can get it to stop from closing if I click on it fast enough, but that stops the code from loading, and any keystroke causes it to close again.
It's the same file every other student downloaded and used with no issue. I've tried re-downloading the file, re-installing python, even trying on a different computer. I've also tried opening it by entering the path file name in the command window with no success. Also no luck trying it in Jupyter Notebook or CodeLab. (And of course, no luck experimenting with the slew of possible solutions on previous Stack Overflow, Reddit, etc. questions).
Any ideas on the potential cause and/or solution?
Edit: cause unknown, but the solution was opening the file with Sypder console.
the file closes immediately because It ended and returned 0, to stop that from happening you can add at the end of the python code something like that:
ending = input("press key to exit")
or just import time and use
time.sleep(5)
you can open your file in the cmd/terminal window and you will be able to see the results

Automate "Right-click + Print" on .xlsx files

I need to automate the act of printing .xlsx file.
I have already seen some answers to this task saying that it is possible by creating a VBA script, as well as some examples. That is not about what my question revolves around.
Thought, I know that it is also possible to right-click on a .xlsx file and click "Print", which does the exact task that I want. It opens Excel, prints the file to the default printer, then closes Excel. (Windows 7, by the way)
So I'm thinking that the work has already been done here.
What process is launched when clicking this "Print" option? Can it be launched via command line, or "clicked" by a python script or something? And if not, why? How can something so easy to click be impossible to automate? I assume a process of some sort must be launched in some way.
Found it!
This task can be easily launched using python.
import os
os.startfile('C:/path/to/the/file.xlsx','print')
This code will launch the same print task. From there, it is pretty trivial for a python developer to automate the task in his scripts.
However, if you do not know much about Python and do not want to learn it now, an easy (or lazy?) way to add it in any automation script would be to save the two lines of code above in a whatever.py file, and launch it via command line (with Python installed, of course).
The context menu print command for Office documents utilizes Dynamic Data Exchange (DDE) and does not directly run a command that can be replicated from the command line.
You can view the content of the commands in the registry. Browse to HKEY_CLASSES_ROOT\.xlsx and look at the (Default) data column. On my machine, "Excel.Sheet.12" is the type of a .xslx file. Then browse to HKEY_CLASSES_ROOT\Excel.Sheet.12\shell\ to see the commands registered for that file type. On my machine, the Print (Default) is "C:\Program Files\Microsoft Office\Office16\EXCEL.EXE" /dde and the "command" is zn=BV5!!!!4!!!!MKKSkEXCELFiles>]-z5hw$l[8QeZZR4_X=$ /dde, none of which is directly useful or accessible for running from a command line.
It will require another program to allow you to access the interface, but there are programs that allow you to make use of DDE from the command line. I recommend Freddy Vulto's Class Exec. More information and a few other similar utilities can be found here.

jupyter lab script to set up view layout

I have some python functions to create graphical interface components in jupyter (lab) using ipywidgets. For my specific application, it's very useful to create different views for different cell outputs containing different widgets. I do this manually with "right click / create new view for output". However, sometimes this process gets annoying when I close everything and come back to work on it later, because I have to redo this layout manually every time and I was wondering if there's a way to write a single script (that I can run from some command line interface) that basically sets this up automatically, i.e. run commands in a jupyter lab session, get specific output cells and send them to different views, etc. Is there a way to do this?

Press enter stops computing python

I'm doing some engineering analysis with the help of a FEA program and Python. When the analysis ends I need to press a key to continue. But this is not a normal press any key to continue. Every code executed with the scripts stops. Like a handput debug break. Nothing runs until I press something or switch windows.
I cant use send keys and subprocesses because running code completely stops. Only solution I could come up with is to use another script in another command window with simple send keys command. This extra script is useless if computer is used or another window is active.
I'm a beginner level programmer and maybe I'm missing something simple. I guess the problem is caused by the FEA programs code but I'm not sure. So is there any way to prevent my code from stopping? Thank you for your time.
It seems that the FEA program does the windowing and you cannot do much about it. I actually automate scripting in DIANA FEA. For this program I would try something like pywinauto.
https://github.com/pywinauto/pywinauto
And call your python script from another python script.
from pywinauto import Desktop, Application
import time
app = Application().start("FEA_program.exe my_python_script.py")
while True:
time.sleep(5)
# send key presses to the app every arbitrary seconds

Jupyter Notebook Copy mechanism

I am trying to make a copy of a readonly jupyter notebook (made by setting the chattr i bit with a 644 permission). The notebook copy fails when the kernel is set to Python2 or Python3. It only opens a blank page. Interestingly a similar notebook with a R kernel successfully makes the copy.
Does jupyter kernels have special calls for this make a copy UI?
As far as I can tell make a copy triggers notebook.js which is handled by handler.py. I put a listener in handler.py to debug this but it seems like the make a copy is not even entering the context. I tried putting in a alert box in the notebook.js as well before line 2781, but that didn't seem to do anything. Any pointers to debug this scenario or how the copy code gets called?
Relevant information:
Jupyter is being served via jupyterhub==0.6.1
jupyter==1.0.0
ipykernel==4.3.1

Resources