Open program files in the Firefox browser window - python

I wanted to open the get-pip.py file in a new tab so that I could view it, but in Firefox, unlike Chrome, I cannot find a way to view code in a tab with an odd extension. When clicking on the code it asks if I want to download it, but I don't want to.
When I selected Firefox as the default program to open it (hoping it would just treat it like a text file or something, the same way Chrome handles odd extensions like .new) new tabs kept opening like a runaway freight train! It was difficult to get it under control and salvage my session.
Does anyone know how I may modify Firefox so that it will treat extensions like .py as a text file and open it in a new tab?

My version of your problem was solved like so: (on Linux)
cp /etc/mime.types ~/.mime.types # Note the dot in front of .mime
Edit ~/.mime.types, and comment out or delete everything but this:
text/plain asc txt text pot brf srt lua py
Add whatever file types you want to force as text/plain. In the above example, I added lua and py, they were not in the original /etc/mime.types.
Restart Firefox.
Note that this is for all apps that look at that/those files, not just Firefox. For example, if you run a web server, depending on the server, it may also affect that. This is why I did it in my home dir rather than /etc.
How I figured it out:
about:config
mime
This shows three settings:
helpers.global_mime_types_file /etc/mime.types
helpers.private_mime_types_file ~/.mime.types
plugin.java.mime application/x-java-vm
In my case I did not have the private file, so I did the copy/edit/restart as above.
Also Note: I can't get this to work for C files.

Related

How to refresh a directory (Windows 10) using python

I just need to refresh a folder.
A hypothetical ideal example would be:
from aModule import refreshdir # fake
refreshdir("C:\path\to\directory")
Context:
I am using Autodesk Desktop Connector, a service that sync data on the cloud with local folders. To avoid expending resources, this tool just checks for new updates when the user opens the file or refresh the directory (so manually). However, in order to automate some operations, I need to refresh the directory with Python. There is no API for this tool.
Thanks in advance! =)
Edit:
New files can be added in the cloud. That's why it is important to refresh the folder. Example:
Before refreshing:
enter image description here
After refreshing:
enter image description here
os.listdir cannot catch those highlited files before refreshing.
Refreshing a directory is not an operating system operation, but a function of the filesystem browser / explorer. A refresh is essentially just reading in the directory contents anew.
Most likely that Adobe tool is hooking into the filesystem functions that do this enumeration of a directory's contents. If this is the case, then the task should be as simple as
import os
os.listdir("C:/path/to/directory")
Keep in mind that backslashes (\) in standard string literals start an escape sequence, i.e. if you wanted to put an actual backslash there, you'd have to write "\\". However Windows will happily use forward slashes as directory separator as well, so you can just use that :-)
To solve this problem I created a script in Python using the pywinauto library to do a manually task that clicks on the file and then clicks on the Sync option.
In this case you'll need to know the name of the files you want to sync. The code was made to AutoCAD Plant 3D project, you'll need to change the path to your files.
from pywinauto import Application
raiz = "C:\\Users\\YOUR_USERNAME\\ACCDocs\\ORGANIZATION_NAME\\PROJECT_NAME\\Project Files\\PLANT3D_PROJ_NAME\\Plant 3D Models"
Application().start('explorer.exe ' + raiz, timeout=10)
explorer = Application(backend='uia').connect(path='explorer.exe', title="Plant 3D Models")
#Plant3DModels is a variable automatically created with the title of the windows opened
explorer.Plant3DModels.set_focus()
# 'Infra-Geral.dwg' is the name of the file that I will Sync
file = explorer.Plant3DModels.ItemsView.get_item('Infra-Geral.dwg')
file.right_click_input()
explorer.ContextMenu.Sync.invoke()

Jupyter notebook: Display dataframe such that clicking cells opens files in file explorer (e.g. Windows explorer)

Is there a way to display a dataframe in a Jupyter notebook such that clicking cells opens a Windows explorer (or any other native explorer) in a given directory?
I tried
def make_clickable(url):
return f'{url}'
df = pd.DataFrame({'dir': '.'}, index=[0])
styled = df.style.format({'dir': make_clickable})
display(styled)
df = pd.DataFrame({'dir': 'file:///C:/'}, index=[0])
styled = df.style.format({'dir': make_clickable})
display(styled)
The former opens a new browser tab in which the Jupyter tree is displayed (at the correct relative location, if I replace . by another relative path).
The latter doesn't do anything. If I right-click open in a new tab, it gives about blank#blocked and if I copy and paste it in a new tab address line, it shows the browser built in explorer, not the Windows explorer.
I tried replacing _blank by _explorer.exe but that doesn't change anything for either variant.
Thus, both options are far from what I want.
(I know, security... but, this is for an in-house tool in a a completely off-line setting)
I know it is somehow possible to open explorers from the browser, because I can just use os.startfile in a Jupyter cell, so I was thinking maybe there is a way to execute arbitrary code when cells of a dataframe are clicked? Maybe this requires some sort of widget?
I could always write an entire Python GUI of course, but I'd really rather not.
I am a bit late but I finally figured it out. Javascript itself is unable to open file explorer for security reasons. However javascript can execute python code inside Jupyter notebook(Jupyter.notebook.kernel.execute) and python can open windows explorer using(os.startfile('.'),subprocess.run etc.).
Below is the code. The linux one works fine as intended but with windows the explorer opens but doesn't move into user focus it also shows the orange beeping light behind the icon show it should be fine.
import pandas as pd
import os
import subprocess
windowsv1 = """
<p onclick="Jupyter.notebook.kernel.execute(`os.startfile('C:\\Users')`)">File Explorer.</p>
"""
windowsv2 = """
<p onclick="Jupyter.notebook.kernel.execute(`subprocess.run(['explorer', 'C:\\Users'])`)">File Explorer.</p>
"""
linux = """
<p onclick="Jupyter.notebook.kernel.execute(`subprocess.call([opener, r'/home/equinox'])`)">File Explorer.</p>
"""
def make_clickable(url):
return windowsv2
df = pd.DataFrame({'dir': '.'}, index=[0])
styled = df.style.format({'dir': make_clickable})
display(styled)
I am not 100% on this but I think this will depend on the browser you are using. Furthermore, I think most browsers will block this kind of behavior and not allow a web page link to open your file browser.
Please, someone, correct me if I'm wrong.
EDIT 2020-08-15
It is possible to do it if you install a Chrome Browser extension.
Just follow the below link. After your code worked for me.
https://www.alphr.com/browse-and-open-folders-files-chrome/
Scroll down to Title "Open Any Local File From Chrome".
Follow all the steps
Don't forget to restart Chrome after installing the browser extension (and the extra installation file)

Add tag to a python file in Windows 10

I am trying to tag my python files in Windows 10. However, it seems not to working. I right click on the file, see the details, go to details tab and then... nothing?
I see some file properties, but I can't change them. The only thing I am able to do is "remove properties and personal information". What do you suggest?
Example:
I have a file which gets numbers as an input from the user and sorts the numbers.
My tags would be "sorting" and "input"
It is my understanding that, out of the box, Windows 10 supports a limited number of file types for tagging. Python files are not among those supported types. Therefore, some additional steps need to be taken.
This article details how to use the free File Meta Association Manager available from GitHub to enable tags for file types that aren't supported out of the box.
Once File Meta Association Manager installer file is downloaded from the repository release page install it.
Under the File Extensions list, scroll down and find .py. Then on the left side, select the "Simple" profile.
Once the simple profile is selected, then click "Add File Meta Handler".
Click the button to Restart Explorer.
Open Explorer and right click on a py file and add tags. Note, I did not need to update the registry for this to work.
What if py isn't in the list?
From this article:
If you can not find a specific file extension in the File Meta Association Manager, no application has registered the file extension so far. If you do associate a file extension with an application ("Always open with ..."), this does not create a registry entry. Therefore, associating an extension with an application is not sufficient that this extension gets listed in the File Meta Association Manager.
To add an extension not listed yet, you have to start the registry editor with administrator privileges, go to "HKEY_LOCAL_MACHINE" → "SOFTWARE" → "Classes" and choose "New → Key" from the context menu.

How to store, find , then import a text file on iPad air 2 using Pythonista?

Using an iPad, I'm attempting to import a text file from the Internet in order to utilize in Python MOOC exercise "hangman" from edx:
For this problem, you will need the code files ps3_hangman.py and words.txt. Right-click on each and hit "Save Link As". Be sure to save them in same directory. Open and run the file ps3_hangman.py without making any modifications to it, in order to ensure that everything is set up correctly.
Thing is, these seem not easy options on an iPad. I managed to copy and paste the hangman.py file into a new Pythonista file, but....
how to handle the large text file?
Where do I store it as a text file, find it, then import it into this iPad program?
No problem on Windows. Apple does not allow a file.open() type operation.
One way you could do this if you don't have access to a Mac/PC, i.e. entirely on your iPad in Pythonista:
Copy the URL of the text file in Safari (tap and hold the link)
In Pythonista, switch to the interactive prompt (swipe from right to left)
Enter the following two lines:
import urllib
urllib.urlretrieve('<paste copied url here>', 'words.txt')
You could also write these two lines in a regular script instead of using the interactive prompt. But you'll probably just need this once.

Get url from webbrowser in python

I would like to ask how can I get list of urls which are opened in my webbrowser, from example in firefox. I need it in Python.
Thanks
Try either SeleniumRC - which is very good
or https://github.com/bard/mozrepl/wiki/
You can use it with python as described here
http://www.craigethomas.com/blog/2009/04/get-android-market-stats-with-python-mozrepl-and-beautifulsoup/
But I would go the selenium route for anything not trivial
First I'd check if the browser has some kind of command line argument which could print such informations. I only checked Opera and it doesn't have one. What you could do is parse session file. I'd bet that every browser stores list of opened tabs/windows on disk (so it could recover after crash). Opera has this information in ~/.opera/sessions/autosave.win. It's pretty straight-forward text file. Find other browsers' session files in .mozzila, .google, etc.. or if you are on windows in /user/ directories. There might be commands to ask running instance for its working directory (as you can specify it on startup and it doesn't have to be the default one).
That's the way I'd go. Might be the wrong one.

Categories

Resources