Python Daily
2.57K subscribers
1.48K photos
53 videos
2 files
38.9K links
Daily Python News
Question, Tips and Tricks, Best Practices on Python Programming Language
Find more reddit channels over at @r_channels
Download Telegram
Running Flask in background (Without Celery)

I am attempting to do some automation work in the background of my Flask application. Basically I get a request and then want to run some boto3 automation infrastructure. Due to the nature of the tasks needing to be synchronous, the connection times out before it can give a response back. I want to simply run this in the background and celery is definitely overkill for this solution. I see that I can use APScheduler and can simply do a one time run based on time. I am wondering I can also do it via multiprocessing. So can I do something like:

@app.route(‘/create’, methods=[‘POST’]
def handle_create_request():
data = some_handler(request)
p=Process(target=create_aws_resources, args=(data,))
p.start()
return “Your resources are being provisioned”

This works locally in my dev, but when running in server implementation, the nature of a WSGI will cause it to be a blocking process. Can anyone give any guidance?

And I feel celery is completely overkill here is due to this process not being run that frequently. It ultimately takes just over a minute to execute and for the most part, it isn’t running that frequently. I know this could be transformed into a microservice down the line, but I am currently looking for a quick solution at the moment.


/r/flask
https://redd.it/7ozw84
How to access Google Drive files from a Jupyter server?

I'm working on a project that involves consolidating 100+ .csv files into one file and running specific queries on this data. However, all of these .csv files are located in Google Drive. I don't want to manually download all 100 .csv files and upload them to my Jupyter server, so I'm wondering if there is a way to access them easily and store them in a folder in the server.

I'm aware of the github code that allows you do to this, but my access was denied, so I'm hoping there's another way to do this.

/r/IPython
https://redd.it/7p85kj
How to run ipython notebook and save straight to pdf w/o viewing?

I've been creating reports using ipython notebooks. When I want to present them to others I print to pdf and then share. Is it possible to run all the cells in a notebook and save the resulting notebook to pdf w/o opening the notebook?

/r/IPython
https://redd.it/7p9ci4
What's everyone working on this week?

Tell /r/python what you're working on this week! You can be bragging, grousing, sharing your passion, or explaining your pain. Talk about your current project or your pet project; whatever you want to share.


/r/Python
https://redd.it/7p7b6z
Collaborating on a project.

Hello! I'm looking to create a project and I wanted to see if anyone else is interested in joining. I've gone through a couple books so I'd consider myself a step above beginner in terms of ability and this project wouldn't be anything serious, just an exercise to get better at Django.



/r/djangolearning
https://redd.it/7pd3k2
[AF] POST request on one of multiple defined endpoints returns 404

EDIT: The error ended up being with NGINX, all I needed to do was change the ownership of the /var/lib/nginx directory from the nginx user to the user running the Flask application. Doh

Hi, I was having the darndest time debugging this and couldn't find anything similar online so hopefully you guys can help me get to the bottom of this

I have two endpoints, a login and a post submitter. Both are GET and POST request endpoints, both are defined, both work locally. Deployed, however, the login endpoint works fine but POST requests on the post submitter endpoint always return a 404. I don't believe it's an issue with NGINX or gunicorn as the logs show nothing out of the ordinary and the login endpoint works flawlessly. I've spent almost two days debugging this and I just can't for the life of me figure it out. Relevant code snippets are posted below.

@app.route('/newpost', methods=['GET', 'POST'])
@login_required
def new_post():
form = BlogForm()
#No code in this conditional is ever actually executed, 404's first
if form.validate_on_submit():
#Omitted code for getting form data and creating form object
try:
#Omitted code for database actions
return redirect(url_for('index', _external=True))
except IntegrityError:
flash("Error inserting into database")
return('', 203)
else:
#Omitted some code for template rendering
return render_template('new_post.html')


@app.route('/login', methods=['GET', 'POST'])
def login():
form = LoginForm()
userAuth = current_user.is_authenticated
if form.validate_on_submit():
flash("Login attempt logged")
user = User.query.filter_by(username=form.userID.data).first()
if user:
checkPass = form.password.data.encode('utf-8')

if bcrypt.checkpw(checkPass, user.password):
user.authenticated = True
login_user(user, remember=form.remember_me.data)
return redirect(url_for('index', _external=True))
else:
form.errors['password'] = 'false'
else:
form.errors['username'] = 'false'

else:
return render_template('login.html',
title='Log In',
loggedIn=userAuth,
form=form)

/r/flask
https://redd.it/7p1g11
Python script as Windows service only working in DEBUG mode

Hi r/python, I'm trying to run a dummy Python script as a service in Windows 7. It just writes the current time to a file in disk. My code goes like this:

import os
import time

import win32serviceutil
import win32service
import win32event
import servicemanager
import socket


class AppServerSvc (win32serviceutil.ServiceFramework):
_svc_name_ = "DummyService"
_svc_display_name_ = "Dummy App"

def __init__(self, args):
win32serviceutil.ServiceFramework.__init__(self, args)
self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
socket.setdefaulttimeout(60)

def SvcStop(self):
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
win32event.SetEvent(self.hWaitStop)

def SvcDoRun(self):
self.ReportServiceStatus(win32service.SERVICE_START_PENDING)
self.ReportServiceStatus(win32service.SERVICE_RUNNING)
servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
servicemanager.PYS_SERVICE_STARTED,
(self._svc_name_, ''))
self._logger.info("Service Is Starting")
dummy()


def dummy():
while True:
dummy_file = open('C:\dummy_text.txt', 'w+')
trace = str(time.time()) + '\n'
dummy_file.write(trace)
dummy_file.close()
time.sleep(40)

if __name__ == '__main__':
win32serviceutil.HandleCommandLine(AppServerSvc)

When I go to the directory containing the script and run *python dummyproc.py install* it tells me *Service installed*. I can debug the process with *python dummyproc.py debug* and it works fine (writes the file). But if I run *python dummyproc.py start* it gives me an error (it tells me that the service timed out against the start or control request). If I go to Windows 7 services and try to run it manually it also tells me the error code: 1053. I've been googling so far and tried many solutions but none of them worked for me. I included python and their Scripts and DLLs folders to the system path, I incremented the ServicesPipeTimeout value on regedit, I tried to install it as my user, tried to run not as a python script but as an executable compiled with pyinstaller... It did not work. Does anybody know what could be happening?? I'm using Windows 7 Professional 64 bits and Python 2.7.13.

Thank you in advanced!

/r/Python
https://redd.it/7pedd5
[Q] Need help in understanding how a part of flask works.

Hi r/Flask

I was following Miguel Grinberg refreshed flask mega tutorial and had a small doubt in one section.

for whatever reason, I was unable to post a comment there so I decided to seek the expertise of you folks here.

so here's the question (and relevant code snippet).

@app.route('/login', methods=['GET', 'POST'])
def login():
form = LoginForm()
if form.validate_on_submit():
flash('Login requested for user {}, remember_me={}'.format(
form.username.data, form.remember_me.data))
return redirect('/index')
return render_template('login.html', title='Sign In', form=form)


I don't completely understand the flow of the program when the login url is visited.

Could someone please explain how the " form.validate_on_submit() " is evaluated before the user enters data into the form and clicks the submit button, which I believe is only possible when the login function has returned and rendered the login template ?

Thanks !

/r/flask
https://redd.it/7pfz3w
Changing jinja2 configuration with flask trim_blocks and lstrip_blocks

So I want to change `trim_blocks` and `lstrip_blocks` to true to remove the ugly whitespace you get when you render a template but I can't seem to figure out how to it correctly. I found that I can make a Jinja environment with my own config but this makes it so I have to do `environment.get_template('index.html').render(var=var)` instead of simply `render_template('index.html', var=var)`. Also with this I need to specify the loader for it to know where to find the templates and it breaks flask-bootstrap.

/r/flask
https://redd.it/7ph5na
What a surprise: had some Python swag in the mail today!

/r/Python
https://redd.it/7pid7p
Has anyone used vaex? Out-of-core dataframes

Recently discovered [vaex](https://docs.vaex.io/en/latest/index.html). I was curious how it compares to using Dask.

/r/pystats
https://redd.it/7pjmi0
Why form.isValid() always returns false ? HELP

/r/django
https://redd.it/7pklwd
What would be an alternative to matplotlib in python for visualisation?

I've been using matplotlib and plotly for visualisation in python for some time. matplotlib feels very primitive, plots look crude. Plotly has been a nice ride but it's not free and had to give it up. Are there any better alternatives?

I'd mostly be working in bivariate analysis and time series analysis and more than often want to produce geographical overlays/plots.

/r/Python
https://redd.it/7pg3nu