How to edit text file through iPython Notebook connected to remote server? - python

I'm exploring a new workflow for interacting with a remote server and iPython has many attractive properties for session logging and scripting. The one thing I have not figured out yet is how to do effective text file manipulation on the remote machine.
I'm aware of the %load and %writefile magic commands, but that's quite awkward for general use.
Is there a way to get %edit to work when using Notebook connected to a remote (*nix) server? Best solution would be an simple editor that opens up in the web browser similar to the panes created by the command_name? help viewer. My search seems to suggest that doesn't exist. I have found this PEP requesting it.
I figure it's probably possible to have %edit trigger an X-windows editor to open up on my local machine, but I don't see how to make that happen. That would be a fine answer if there's a way to do it.
I should also clarify that I'm not generally interested in executing python code in the files, I just want to edit the file. i.e. %edit -x would probably be my general use case.

Related

What tools to deploy webserver running numpy behind?

If I am given a dataset (say excel file), I would like to deploy a webserver on which I upload this excel file, it runs python/numpy code and displays some figure.
I could also have some checkboxes on the website that would change the parameters in the code.
What tools would you recommend for doing that? What would be the easiest way?
For now I have the python code on jupyter notebook and use:
from ipywidgets.embed import embed_minimal_html
embed_minimal_html('export.html', views=[fig])
to see my figure (fig) locally, by running: python -m http.server 8080
I would like this to be deployed and choosing the file to upload.
In my opinion Django may be the best option for you. It requires Python knowledge and basic HTML/CSS -for basic usage-, that is why I thought it is the best option.
You can develope python scripts and user can able to modify the input for that python script and easily create an dashboard with graphics/calculations.
You may want to check Vitor's website:
Simple is the Best by Vitor Freitas
Use a Github repo to host your code, and then share it via MyBinder.org. An example, that is simpler than yours, but like what you describe in some ways is here. When you get there click the launch badge and a temporary session will spin up. In the notebook that comes up, you can do Run All Cells under the Run menu. The session dies after 10 minutes of inactivity. You can download useful information. See more about the MyBinder project here. I am taking advantage of the drag and drop for file upload that comes with JupyterLab here but there are file upload widgets (see below).
That example one doesn't have fancy widgets like toggles to make the choices but you can add them. For example, see the appmode demo. (Click launch binder button on the page.) You'll see you can have the widgets in the notebook or in the 'appmode'. You can make the interactions with the widgets fancier, too. Some examples featured in the Voila gallery will give you more of an idea of what is possible with the widgets and communicating to your underlying python. All those are on Github and served via temporary sessions from MyBinder.org. Keep in mind those apps can run in the notebook if you want that. The widgets work there in the notebook versions as well, too. And you can directly link to the notebook mode if you prefer, as shown under the heading 'Direct links to start out in notebook mode' here.

localhost server in compiled Python program

Background:
I am still new to Python so apologies if this is a naive question. I was originally looking to develop a tool for Windows with a GUI, however, reading around I get the impression that it may be simpler to have my compiled python code somehow 'wrapped' in simpleHTTPServer so users can interact via localhost (much like Jupyter notebook/labs).
Question:
Is there a standard/recommended way to have a compiled python program interface with the user via a localhost port in their web browser? I assume this is a fairly common approach and that a package may already even exist but I've not been able to find anything that gives guidance on how to tackle it.
Alternatively, is is feasible to somehow compile a Django development project to run on localhost?
Ideally, I want an executable file that users can run and their browser opens up at the localhost port required.
If you wish to quickly make a GUI based Python tool, you should consider using Django, a very straightforward framework for WebApps. It is very quick to learn and implement, and you shall very easily be able to integrate highly advanced functionalities too into your tool with ease.
When it comes to distribution of code, you can convert the entire Django App into a windows exe file for ease of distribution and use. There are many python packages that allow you to port python to windows executable files. I would recommend using PyInstaller. You may find this link useful: Recipe Executable From Django.
Alternatively, as someone suggested, you may use Docker, but that would be heavier in terms of space utilized, and will require the user to install the same.
Hope this helps!

Simplest Way to Serve Jupyter Incubator Dashboards Locally

I've been exploring steps one and two from the Jupyter Incubator Dashboards project (more information here: https://github.com/jupyter-incubator/dashboards/wiki). Its simple to turn my notebooks into usable dashboards, which is just great.
My problem is that I'm unsure about step three, which is sharing/hosting the dashboards. I'm in an enterprise environment with a local server. I need to be able to share the dashboards on the local server and then have people access them over the web. Essentially, I need the "deploy as a local dashboard" option from step two (dashboards_bundlers). It seems that this functionality is now deprecated, however.
My question is this, is there a simple way to share dashboards locally here? I've been looking at the dashboards_server application, but it looks a little over my head right now. The functionality here looks powerful, but is probably more than what we need for the time being.
If this is the only option then I will proceed there, but I wanted to see if anyone has a simpler solution first.
As I see you have posted the similar question on the Github Issues section of Jupyter
You have received the answer too but is not satisfactory I guess. As the person have mentioned that there isn't any way right now for achieving what you are expecting.
But if you want that others should access the local copy of your system Jupyter Dashboard then it is better you make your system as server itself. Then you can grant access to the people for viewing your server/local machine. Others can be a child like systems for your local system.
Just like a Parent-child or Say Master-slave would allow others to have access to your Jupyter DashBoard Copy.
It might be a bit confusing but this is way you can achieve whaht you have mentioned in your question.
This might be a too simplistic answer. Our company uses Windows 7 enterprise. I have Jupyter Notebook running on a desktop which I access by running the command:
jupyter.exe notebook --ip=10.106.166.180 --no-browser
I just use the IP address of my host computer.
Then I just send people the ipynb url
example http://10.106.166.180:8888/notebooks/Notebooks/sample.ipynb
As long as you don't mind people tinkering, using or snooping around your notebooks. Also if the time comes, you can always restart the server then add a password so that people can't access it anymore.

Local development (invoking python <script>, port 8888) serves stale files

I had a similar issue when running fast-cgi and I was told there is no way to fix it: Files being served are stale / cached ; Python as fcgi + web.py + nginx without doing custom work. I was told to use the python method, which invokes a local "web server" to host the python page.
Even doing that, the files served are stale / cached. If I make edits to the files, save and refresh, the python web server is still serving the stale / cached file.
The only way to get it to serve the modified file is to kill (ctrl+c) the script, and then restart...this takes about 5 seconds every-time and seriously impedes my development workflow.
Ideally any change to the script would be reflected next time the page is requested from the web server.
EDIT
#Jordan: Thanks for the suggestions. I've tried #2, which yields the following error:
app = web.application(urls, globals(), web.reloader)
AttributeError: 'module' object has no attribute 'reloader'
Per the documentation here: http://webpy.org/tutorial2.en
I then tried suggestion #4,
web.config.debug = True
Both still cause 'stale' files to get served.
Understandably you want a simple, set it up once and never worry about it again, solution. But you might be making this problem more difficult than it needs to be.
I generally write applications for an apache/modwsgi/nginx stack. If I have a caching problem, I just restart apache and voila, my python files are re-interpreted. I don't remember the commands to restart apache on all of my different boxes (mac's, ubuntu, centos, etc), and I shouldn't need to.
That is what command line aliases are for...
A python application is interpreted before it is run, and when run on a webserver, it is run once and should be considered stateless. This is unlike javascript running in a browser, which can be considered to have state since it is a continually running VM. You can edit javascript while it is running and that is probably fine for most applications of the language.
In python you generally write the code, run it, and if that doesn't work you start over. You don't edit the code in real time. That means you are knowingly saving the source and changing contexts to run it.
I am betting that you are editing your source from a Graphical IDE instead of a command-line editor like vi or emacs (I might be wrong, and I'm not saying there is anything 'wrong' with that). I only write iOS applications using an IDE, everything else I stick to ViM. Why? Because then I am always on the command line, and I am not distracted by anything (animations, mouse pointers, notifications). I finish writing my code, i quickly type ':wq' (write and quit), and then quickly type 'restartweb' (actually i usually type 're' then <\tab> to auto-complete) which is my alias to whatever the command to restart apache is. Voila my python is reinterpreted.
My point is that you should probably keep it simple and use something like an alias to solve your problem. It might not be the coolest thing you could do. But it is what Ninja coders have been doing for the last 20 years to get work done fast and simple.
Now obviously I only suggested a solution for apache, and I have never used web.py before. But the same possible solution still applies. Make a bash script that goes in your project directory, call it something like restart.bash. In it put something like:
rm -r *.pyc
Which will recursively remove all compiled pyc files, forcing your app to reload. Then make an alias in your ~/.bashrc that runs that file
Something like:
alias restartproject="bash /full/path/to/restart.bash"
Magical, now you have a solution that works everywhere, regardless of which type of web server you choose to run your application from.
Edit:
Now you have a solution that works everywhere but on a Windows IIS server. And if you are trying to run python from Windows, you should probably Stahp! hugz
We are using virtualenv right? :) We want to keep our python nice and system-agnostic so we can sell it to anyone right? :) And you should really check out ViM and emacs if you don't use them... you will bang your head against the wall for a week getting used to it, then never want to touch a mouse again after that.
Right, so Python is a compiled language when run on a web server. It's outputting a .pyc file that's the compiled version. Your goal is to tell the web server that the .pyc file is out of date and is no longer valid.
You have a few options:
Delete the relevant .pyc file
For web.py, use the reloader middleware
Send it a HUP signal (I'm lazy and usually do killall -SIGHUP python). You can do this automatically with a file watching tool like watchdog (thanks barracel).
web.config.debug = True should be the default in your application
None of those options are working for you?

Where can I find a browser based editor for python that works with IE7?

I'd like to get some code editing for a free software project done during some free time that I will have today at my university. There's just one small problem: We're almost entirely a windows environment (mostly thin clients at that) and the only source code editor installed is jGRASP.
So, I need a source code editor which is usable without installing anything (something browser, or Java Web Start based). I would use Notepad++, but I think downloading and running some unapproved exe may be in violation of our acceptable use policy, and it isn't that efficient of an editor for python anyway.
Any suggestions are appreciated.
The best webbased editor I know is Ace.
If you can't execute downloaded exes, this maybe mute, but SciTe is reasonably useable and does not require you to install anything - just download and run.
Putty and vim would be my solution (Requires a remote host running 'nix)
Putty is obviously a 3rd party binary, but a fairly common one.
I'm sure there are web based ssh clients to be had.

Categories