Interactive Shell on website in django - django

I have a Django project up and running. I'd like to provide a user to interactively use JDB through the web app to debug an application. I want that the user can issue a command such as
stop in [function name]
next
And that he would get the back the response and then can proceed.
When I use os.system however, like so:
def AttachDebugger(pid):
# enable port forwarding
adb = ADB()
adb.set_adb_path('~/Library/Android/sdk/platform-tools/adb')
adb.forward_socket('tcp:8001', 'jdwp:' + pid)
# attaching
os.system('jdb -connect com.sun.jdi.SocketAttach:hostname=localhost,port=8001')
then it opens up a JDB session in the background, but it freezes there and doesn't return control to the python code except stopping it via CTRL+C. Is there any way to provide such an interactive session?
What I would like to have is similar to the GITHUB-Tutorial-Page here:
https://try.github.io/levels/1/challenges/1
where the user can interact with the JDB instance
Thanks in advance.

I got it done with the pexpect module.

Related

How do you launch a Tkinter application from a django website

I created a Django website and I want to launch a tkinter program if the user clicks on a button. Can anyone tell me how to do that?
Any help is appreciated.
Generally, websites cannot cause other programs on the user's computer to run just by clicking a button on a webpage. The next-best thing is a specialized link that the client understands is associated with another installed application. See: How can I launch my windows app when a users opens a url?
If you happen to be running the django server on the same local system AND is running in the foreground by the same currently logged in user, you could also invoke a GUI program this way, as well for example using subprocess or os.system. Though, this would be quite an odd way to do this.
def my_view(): # Technically works, but in very limited practical use cases.
subprocess.Popen(['python', 'my-tk-app.py'])
Because tkinter also doesn't like to be run in any other thread besides the main thread, and other event loop-related issues, it's not really practical to invoke a tkinter app directly from a Django view, which usually will be running in a separate thread and needs to not be blocked.
For example, this would not work:
def my_view():
root.mainloop() # start the TK app
# fails because tk only wants to start in the main thread
# even if TK _could_ start, the view would eventually timeout
If you need a local tkinter app to also interface with a django server, you're almost always going to be better off by making your GUI app a client of the server -- that is to say, your tkinter app should use HTTP(S) or similar mechanism to communicate with Django.

How do I run Python code entered on a browser on Django views?

I have built a platform on django for running python scripts. The user is able to write their Python script in a text-area and when a button is clicked, the script should run on the back-end. It should also check for test cases from a database (like Hackerrank).
I have tried using the eval() function but the problem is that when the user writes input() in their code it should take the values from database for input.
Please help me figure this out.

How to add logs in django

I am new to django I want to print log information in my console how to do that. and also what is the use of that things in django, why we need that one.
If you want to log stuff to your console, javascript's console.log() would be the way to do that. With Django, print statements will show up in your terminal window where the server is running.

How to utilize python script in DJANGO

I have built a python parser that lets the user enter certain search parameters and then searches through a set of syslogs looking for specific information. When there is a match it writes the lines to a separate text document so that the user can review/save the output. All works great, but as of now its all done via CLI on a local PC.
The next step in my project I want to be able to host a web server whereby users can upload their syslogs from their PC to a front end website, and have my python parser do a search based on their search parameters and then print the result to the webpage. Is this possible? I have been playing with DJANGO a bit, but its becoming clear that this might not be the best route to go.
I was thinking maybe using the Python HTTP simple server as an alternative?
Django vs Simple python HTTP doesn't matter, to run a command line program from the python interpreter is simple just use
import os
os.system("curl X")
To run the python script just move the script to where your HTTP stuff is, and import it
from SCRIPT import FUNCTION
FUNCTION()

How to make django do user independent or session independent tasks?

I am working on a project which periodically downloads stock details as a json file from a web server and save locally for user requestes .Since it is not appropriate to download the same for each user request i need help in doing that
I know how to do it using cron but i prefer it in django itself ..
Write a django command using while True loop and thread.sleep(intervaltime) function.
And after starting django server, run this python manage.py commandname