Automatically update database in django - python

I am trying to build a website that displays stock information and I have a file called populate_stocks.py that populates the database with a given set of stocks. Since these stocks change almost every minute, I need to make sure I update the database with new information by running populate_stocks.py again.
I was wondering if there is any way to let my django application automatically call this file to update the stock information. I searched around and found another person using crontab which seems a bit complicated and was wondering if there is another solution.

Though I agree that the use of crontab to solve this problem is actually probably the easiest solution to this problem, there is a possible alternative over crontab which would be to make a management function that runs alongside your server.
Basically in its absolute simplest form you would make a basic loop in the form of a Django NoArgsCommand.
from django.core.management.base import NoArgsCommand
from populate_stocks import yourmysticalfunctionofupdating
class Command(NoArgsCommand):
help = "This runs the loop of glory, that does as it is told."
def handle_noargs(self, **options):
while True:
yourmysticalfunctionofupdating()
You would need to put this into a management -> commands folder and name the python file whatever you want the command should be (imagine it is updatify.py in this example).
You could then run the following command to run your watchdog.
./manage.py updatify
Though this may be overkill for your particular problem I have found it very helpful for trickier issues, and I hope it saves someone some time.

Related

Run a file.py in django

I have a Django project, in which I have already defined the models.
I write a file called my_logic.py, which contains custom logics for my app; for example, it SQL-ly queries the relevant data from the database, formats that data, and sends them to an online ML-model to get prediction.
I would like to automatically run this file every hour. Is there any way to achieve this? I think the possible answer is cronjob.
Thanks

Python/Django - How to debug variables

I'm pretty new to the Python/Django world and I need to dump some variables (right after model calling for example) and display all the informations about that specific variable to the user (the developer, in my case, me).
In PHP we are used to do "var_dump($some_var); die;"
But in my case, I can't find a way to achieve just that, and I'm pretty sure that's simple because obviously every django/python developer are able to do that !
Just write the following line after the model
‘import ipdb; ipdb.set_trace()’
For more information see this tutorial

Track changes in SQL Server tables using Python

I am new to Python and I am trying to figure out to integrate Python into my workload. I have a set of tables within SSMS 2014 that I want to track on a daily basis. If there are any changes to the data elements within each table, I want to track that and log that there was a change to it. I want to have that run automatically every morning without me having to run the script manually. I figured that creating some sort of a script/function in Python would be best to run all the tables that I want against it. Are there any packages, templates, etc out there that work best for this kind of situation? Thanks in advance.

How to keep track of a continously changing file when being used by multiple users

I apologize if the question is a little vague but I'm trying to find the simplest way to do this.
I have a small group of people, for whom I have written a python script. Now this python script summarizes articles mined from a website (that are unique by an id number) when the user runs it with some parameters. Now each user might choose to "claim" one or more articles, which means that they will be working on it. Thus any future execution of the script should omit using a "claimed" article in its summary.
I need a way to have a globally accessible file, which my script accesses and checks its output against.
I also need to have a way for the user to add multiple id numbers to this global file.
I understand that a rudimentary database might be the best way to go, but is there a simpler way to read and edit files remotely over python? I can host this file on my personal webspace, but I'm not sure of what would be the simplest way to edit and read it since I'm relatively new to python.
The number of users is small and constant so it does not have to be very robust, just needs to work.
Language: Python
Thanks!

How can I run my python script from within a web browser and process the results?

I have a written a short python script which takes a text and does a few things with it. For example it has a function which counts the words in the text and returns the number.
How can I run this script within django?
I want to take that text from the view (textfield or something) and return a result back to the view.
I want to use django only to give the script a webinterface. And it is only for me, maybe for a few people, not for a big audience. No deployment.
Edit: When I first thought the solution would be "Django", I asked for it explicitly. That was of course a mistake because of my ignorance of WSGI. Unfortunately nobody advised me of this mistake.
First off, is your heart really set on it being Django? If not I'd advise that Django, whilst an awesome framework, is a bit much for your needs. You don't really need full stack.
You might want to look at Flask instead, which is a Python micro-framework (and dead easy to use)
However, since you asked about Django...
You can create a custom Django command (docs here) that calls your script,
that can be called from a view as described in this question.
This has the added benefit of allowing you to run your script via the Django management.py script too. Which means you can keep any future scripts related to this project nice and uniform.
For getting the results of your script running, you can get them from the same bit of code that calls the command (the part described in the last link), or you can write large result sets to a file and process that file. Which you choose would really depend on the size of your result set and if you want to do anything else with it afterwards.
What nobody told me here, since I asked about Django:
What I really needed was a simple solution called WSGI. In order to make your python script accessible from the webbrowser you don't need Django, nor Flask. Much easier is a solution like Werkzeug or CherryPy.
After following the django tutorial, as suggested in a comment above, you'll want to create a view that has a text field and a submit button. On submission of the form, your view can run the script that you wrote (either imported from another file or copy and pasted; importing is probably preferable if it's complicated, but yours sounds like it's just a few lines), then return the number that you calculated. If you want to get really fancy, you could do this with some javascript and an ajax request, but if you're just starting, you should do it with a simple form first.

Categories

Resources