How to run a script shell in django - django

I am developing a web app using django. I've a script shell that runs python codes for image processing. I want to execute this script shell using my web app so that users can apply image processing just by using a button in the web interface.
(for the moment i'm working localy , the django web app and the script shell are in the same folder).
I don't know how to do it. can you help me ?
thank you in advance.

Python standard library has the subprocess module which can be used just for that. For instance, given ./script.sh is your shell script a call to subprocess.call will do the work:
>>> import subprocess
>>> # From your working directory:
>>> subprocess.call(["./script.sh"])

Related

Running Flask in Pycharm but can't enter custom commands [duplicate]

I created new flask project in PyCharm but I can't see how to run flask shell in integrated PyCharm python console window.
Name app is not defined when start console:
I still can run command "flask shell" in integrated PyCharm terminal but it will start without code completion, hints, syntax checks, etc.
Flask app is defined in terminal:
Is there any way to start flask shell in integrated PyCharm python console?
You can use a combination of creating a request context for the Shell (which flask shell does) and a custom starting script for the PyCharm console.
# Step 1: acquire a reference to your Flask App instance
import app
app_instance = app.create_app()
# I return app instance from a factory method here, but might be be `app.app` for basic Flask projects
# Step 2: push test request context so that you can do stuff that needs App, like SQLAlchemy
ctx = app_instance.test_request_context()
ctx.push()
Now you should have the benefits of flask shell alongside the code completion and other benefits that IPython Shell provides, all from the default PyCharm Python Shell.
In my case (in my server.py I had app=Flask(__name__)) I used
from server import app
ctx = app.app_context()
ctx.push()
Then added this to File | Settings | Build, Execution, Deployment | Console | Python Console starting script
This also works:
from app import app
ctx = app.test_request_context()
ctx.push()
more on the Flask docs page
Running a Shell
To run an interactive Python shell you can use the shell command:
flask shell
This will start up an interactive Python shell, setup the correct
application context and setup the local variables in the shell. This
is done by invoking the Flask.make_shell_context() method of the
application. By default you have access to your app and g.
It's just a normal interactive Python shell with some local variables already setup for you, not an IDE editor with code completion, hints, syntax checks, etc.

Python 2.7: No module named hexchat

I'm trying to write a Python script that does something with the inputs from the IRC chat that is connected to twitch.
However, when I do import hexchat, python keeps telling me there is no module named hexchat. I'm using Pycharm btw. I just want to be able to use the functionality in the hexchat module. Any help would be appreciated!
You need to first download and install Hex Chat module from here Downloads and then do the import hexchat.
For Linux - sudo apt-get install hexchat
Documentation for Python API is here Documentation
You load scripts inside hexchat. Window > Plugins and Scripts > Load.
I was having this issue on Windows 10 but I managed to resolve it by loading .py scripts in Hexchat like the below:
/py load "C:\where\your\file\is.py"
You also want to have module info in your .py file too otherwise Hexchat will complain:
__module_name__ = "helloworld"
__module_version__ = "1.0"
__module_description__ = "Python module example"

Shell script to update DB via flask

I got started with flask and I tried out the Flaskr example. On the execution of a certain python script, I would like to update one row of my database.
I am a newbie here and would like to understand: am I going to update the DB from inside that python script or I am going to wait for a signal from the flask WSGI script:
I have referred to this thread but am not sure how I am going to interact with the external script. Any help or hints are appreciated.
WSGI handles HTTP requests/responses. A script won't be issuing those. Instead, import your Flask app in the script and make an application context:
from my_project import my_app
ctx = my_app.app_context()
ctx.push()
# ... my code
db.session.commit()
ctx.pop()
Relevant docs: http://flask.pocoo.org/docs/appcontext/, http://flask.pocoo.org/docs/shell/
Or consider using Flask-Script to add command line functions to your application, if the function doesn't need to be a separate script.

How to run a series of manage.py commands in Django?

I have a Django project. Everytime I deploy, I need to run a series of manage.py command (such as syncdb, south migiration, fixture update).
I am getting tired of type the command line by line and therefore I wrote a python script to do these:
import subprocess
subprocess.call(['python', 'manage.py', 'syncdb'])
#Skip the detail
subprocess.call(['python', 'manage.py', 'loaddata', 'setup/fixture.xml'])
I am wondering if there is a better way to do this?
Thanks.
You can use fabric, a Python library that allows you to script remote actions. This question has some links in the accepted answer for more information on fabric and django.
You can also call management commands directly:
from django.core.management import call_command
call_command('syncdb')
call_command('loaddata', 'setup/fixture.xml')
Save that as a normal python file and execute it from your shell or as part of your deployment scripts.

Executing shell commands from a Django app/project

I want to be able to execute some shell commands like mkvirtualenv and createdb and such from a Django app, these processes will likely go into a Django celery task later. I would think using system() would be insecure to execute shell commands from Django/Apache, so my question is what is the best way to execute shell commands from a django app?
To start a shell process in the background you can use the subprocess module.