|
I decided to write a Flask intro for anyone that is curious. Why Flask? In my opinion, Flask feels like just another package you would want to use on a project. It is simple to use and very modular. You only add new functionality – such as support for login, if you need it. It also does a great job of letting you write almost solely Python. For the backend, the database can be created without having to write SQL statements. On the frontend, forms and other page content can be created with Python instead of JavaScript. If you would prefer not to write CSS/HTML you can find pre-made static templates that are easy to integrate. Finally, it’s nice to be able to not have to deal with PHP. How to get started Both the book and tutorial are resources by the same author and they are awesome. I would recommend reading through both. Flask Book Flask Tutorial Flask Documentation What can it be used for? I’ve used it to build simple static websites and as a way to serve content, specifically images from a security camera, from my server. Additionally, I followed a tutorial for building a blog. For my new job, I’ll be building dashboards for scientists to access and analyze their data. Basic Demo Setup The basic setup steps are to install a virtual environment, activate it, and then install Flask. code:
The directory stucture consists of the folder for the virtual environment, hello.py which is defined below, and a templates directory. Templates are written in HTML and data from databases or other sources can be added to them before they are returned to the end user as a webpage. code:
The code below starts with declaring a new instance of Flask named app. To the app we add routing which is how we figure out what information we want to send back to the user. The first route, '/' which points to index() will return "Hello World" when a user visits https://www.website.com/. The second route is a little special. If someone visits https://www.website.com/user/huhu, the string 'huhu' will be passed to the user_page() function. Additionally user_page() also has a current_time which gets the current time. These two bits of data are then added to the 'user_page.html' template with the render_template() function. code:
This template takes username and current_time from the user_page() function above, adds the data to the template, and then returns it to the user. code:
The last step is to simply run the following command and the app will start: code:
|
# ? Mar 30, 2017 18:04 |
|
|
# ? Apr 23, 2024 11:40 |
|
Linked to huhu's post in OP.
|
# ? Mar 30, 2017 18:21 |
Just getting started with pandas, so this is probably a really naive question. I'm trying to replicate a really basic algorithm from VBA for oncology forecasting. There is a data file with new patient starts and anticipated duration of therapy in monthly cohorts. Each new patient cohort loses a certain amount of patients each time period using an exponential function to mimic the shape of a kaplan-meier progression curve. The sum of a column represent the total remaining patients on therapy across all cohorts. Below is the code I'm using:code:
KingNastidon fucked around with this message at 06:15 on Mar 31, 2017 |
|
# ? Mar 31, 2017 02:17 |
|
Ok here's a dumb question: I want to make a unicornhat (which uses a Python library) on a raspberry pi do something when a new order is placed, and according to the Shopify API, you would use their webhook to do it and you supply them with the URL for the webhook to deliver a HTTP POST payload to in json format. I understand the concept of json stuff after working with the discord API but I've never worked with webhooks before, so correct me if I'm wrong but does that mean I have to have my own server running and listening for the HTTP POST payload? Since it's a raspberry pi would it be easier to try to sniff for like an email notification to trigger the light_sign action? Any pointers would be great, I'm doing this for a friend's company in exchange for some of the neat t-shirts that he sells so I'd really like to find a way to make it work that doesn't take me a million years to figure out
|
# ? Mar 31, 2017 05:53 |
|
KingNastidon posted:Just getting started with pandas, so this is probably a really naive question. I'm trying to replicate a really basic algorithm from VBA for oncology forecasting. There is a data file with new patient starts and anticipated duration of therapy in monthly cohorts. Each new patient cohort loses a certain amount of patients each time period using an exponential function to mimic the shape of a kaplan-meier progression curve. The sum of a column represent the total remaining patients on therapy across all cohorts. Below is the code I'm using: Python is extremely slow and extremely RAM hungry. This is a consequence of aspects of its type system and design. The interpreter has no way to know what to do for any operation without going through a dance of type lookups. It also can't prove that those operations are the same from loop to loop, so it has to redo all that work for every element. The trick to get acceptable performance is to arrange it so that none of the actual computation is done in python (so it is fast) and none of the data is saved as python objects (so it is RAM efficient). You essentially have a bunch of python glue code that is telling external libraries what to do in broad strokes, with the actual computation done by not-python. The main library you use to do this is numpy. Pandas is using numpy arrays as the storage for its DataFrame and Series values A line like: code:
Something like: code:
A piece of code like this: code:
Basically any time you loop over your data, the performance will be crap. Your options are: 1. Rearrange your code so that it's asking numpy to do bulk operations instead of doing them in python. This can be hard/impossible to do and isn't usually how people think about their problems. 2. Write your algorithm that's hard to express in C so that it's a new bulk operation you can do. Alternatly, dig through libraries to see if someone else has already done this for the algorithm you want. 3. Use numba. numba is a python-like sublanguage where enough python features are removed and types are locked down that it can be efficiently compiled. Depending on your algorithm rewriting it in numba could require zero work or could be more annoying that redoing it in C. (The example you posted would be on the zero work end)
|
# ? Mar 31, 2017 07:59 |
|
KingNastidon posted:Just getting started with pandas, so this is probably a really naive question. I'm trying to replicate a really basic algorithm from VBA for oncology forecasting. There is a data file with new patient starts and anticipated duration of therapy in monthly cohorts. Each new patient cohort loses a certain amount of patients each time period using an exponential function to mimic the shape of a kaplan-meier progression curve. The sum of a column represent the total remaining patients on therapy across all cohorts. Below is the code I'm using: Basically I was going to say what Foxfire_ said; for loops in Python are slow and usually aren't what you want to use for bulk-computation. VBA is a compiled language, but Python is not. The first-order speedup you can apply to code like this is to use vectorized numpy operations instead of for loops. code:
|
# ? Mar 31, 2017 08:47 |
|
QuarkJets posted:Basically I was going to say what Foxfire_ said; for loops in Python are slow and usually aren't what you want to use for bulk-computation. VBA is a compiled language, but Python is not. Pandas indexing is insanely slow compared to the underlying arrays Also vectorize your poo poo Or don't and use numba its a tossup as to which ends up faster
|
# ? Mar 31, 2017 11:59 |
|
This has little to do with python being compiled or not: pandas has a ton of overhead if you don't do things the correct way
|
# ? Mar 31, 2017 12:00 |
|
I've been using wrappers like this if I'm working with dataframes and the speed is a limitation. Lets you use Pandas' descriptive row and column indicies instead of integers, while maintaining the speed of working with arrays. Pandas DFs can be orders-of-magnitude slower than arrays.Python code:
Dominoes fucked around with this message at 12:09 on Mar 31, 2017 |
# ? Mar 31, 2017 12:06 |
|
KingNastidon posted:Just getting started with pandas, so this is probably a really naive question. I'm trying to replicate a really basic algorithm from VBA for oncology forecasting. There is a data file with new patient starts and anticipated duration of therapy in monthly cohorts. Each new patient cohort loses a certain amount of patients each time period using an exponential function to mimic the shape of a kaplan-meier progression curve. The sum of a column represent the total remaining patients on therapy across all cohorts. Below is the code I'm using: I don't fully understand the algorithm but the first step is to try to do this with vectors operation. This might not work as written but may only need small tweaks. At least it should illustrate the idea of vectorizing the operation. Python code:
|
# ? Mar 31, 2017 12:16 |
|
Malcolm XML posted:This has little to do with python being compiled or not: pandas has a ton of overhead if you don't do things the correct way Well there you go, I didn't know that because I don't use pandas (like at all)
|
# ? Mar 31, 2017 14:01 |
|
funny Star Wars parody posted:Ok here's a dumb question: I want to make a unicornhat (which uses a Python library) on a raspberry pi do something when a new order is placed, and according to the Shopify API, you would use their webhook to do it and you supply them with the URL for the webhook to deliver a HTTP POST payload to in json format. Yes, that is correct. Your URL should point to a live server that is expecting a POST with the format that Shopify sends you. I don't know any specifics of Raspberry, so I can't really help you with your second question.
|
# ? Mar 31, 2017 14:16 |
|
funny Star Wars parody posted:Ok here's a dumb question: I want to make a unicornhat (which uses a Python library) on a raspberry pi do something when a new order is placed, and according to the Shopify API, you would use their webhook to do it and you supply them with the URL for the webhook to deliver a HTTP POST payload to in json format. Yeah, you need a server running. Basically shopify will call an url on a server you control when orders are placed. Then your code does something when that happens. It will work fine on a raspberry pi. Flask and Django both are both capable of doing this is like 20 lines of code or something. The hardest part will be learning how to do it because for some reason understanding how the web works is not easy despite it being pretty simple.
|
# ? Mar 31, 2017 16:30 |
|
Thermopyle posted:Yeah, you need a server running. Basically shopify will call an url on a server you control when orders are placed. Then your code does something when that happens. It will work fine on a raspberry pi. This is the perfect use case for Flask. You don't need the bells and whistles of Django. Python code:
|
# ? Mar 31, 2017 17:28 |
|
Tigren posted:This is the perfect use case for Flask. You don't need the bells and whistles of Django. FWIW, it's about the same in Django. Flask is fine and there's a 50/50 chance I'd use it for the project myself, but occasionally I like to point out that people have this idea that Django is really complicated or something. It includes a lot more "in the box" than Flask, but it'd work basically just like Flask in this case. Single file, basically same # lines of code, etc.
|
# ? Mar 31, 2017 18:25 |
|
I got promoted to developer recently and my first project has been building a file directory browser in php and Ajax and HTML and it's a fuckton to take in in two weeks so ya idk what it is with web stuff that is so hard to learn (probably all the acronyms and different languages/frameworks interfacing) but ya appreciate it! Flask looks like the way to go 😊 thanks!
|
# ? Mar 31, 2017 19:13 |
|
condolences
|
# ? Mar 31, 2017 23:12 |
|
Dex posted:condolences week 2: i genuinely lust for death
|
# ? Mar 31, 2017 23:21 |
|
funny Star Wars parody posted:Ok here's a dumb question: I want to make a unicornhat (which uses a Python library) on a raspberry pi do something when a new order is placed, and according to the Shopify API, you would use their webhook to do it and you supply them with the URL for the webhook to deliver a HTTP POST payload to in json format. Echoing the other replies that Flask/Django are perfect for this. On the operational side, check out Zappa for some sweet serverless action. This will let you use some AWS services(API Gateway, Lambda) to run the application code without having to keep a server online. You'll fall well within the free tier for development usage, and probably well into their production load as well.
|
# ? Mar 31, 2017 23:52 |
|
Thanks for reminding me about Zappa. I keep meaning to try it out on my next project and then forget. edit: oh yeah now I remember why I really haven't tried it. No Python 3 support.
|
# ? Apr 1, 2017 00:40 |
|
*Disregard, found answer in OP.
Hughmoris fucked around with this message at 00:44 on Apr 1, 2017 |
# ? Apr 1, 2017 00:41 |
|
Thermopyle posted:Thanks for reminding me about Zappa. I keep meaning to try it out on my next project and then forget. Well, the real problem is that Lambda doesn't have Python 3 support. It's pretty annoying, but for some projects, might work fine.
|
# ? Apr 1, 2017 00:49 |
|
I use Python 2.7 anyways so yayyy Edit: how much does Zappa cost amper month for a server for a single webhook that is triggered maybe 5-10 times per day? FAT32 SHAMER fucked around with this message at 00:57 on Apr 1, 2017 |
# ? Apr 1, 2017 00:52 |
|
funny Star Wars parody posted:I use Python 2.7 anyways so yayyy The pricing depends on Amazon Lambda's pricing, but you will probably be well within the free tier.
|
# ? Apr 1, 2017 01:03 |
|
funny Star Wars parody posted:I got promoted to developer recently and my first project has been building a file directory browser in php and Ajax and HTML and it's a fuckton to take in in two weeks so ya idk what it is with web stuff that is so hard to learn (probably all the acronyms and different languages/frameworks interfacing) but ya appreciate it! Flask looks like the way to go 😊 thanks! Re Flask vs Django: Django for websites, Flask for other things you need a webserver for. Flask becomes a pain once you start adding plugins for things like admin, auth, migrations, databases etc. Dominoes fucked around with this message at 02:35 on Apr 1, 2017 |
# ? Apr 1, 2017 02:32 |
|
Dominoes posted:Re Flask vs Django: Django for websites, Flask for other things you need a webserver for. Flask becomes a pain once you start adding plugins for things like admin, auth, migrations, databases etc. Outside of a very specific library (Flask-Social), I had no issues with any Flask extension so far. I'll give you that it's easier than Django to make a mess of packages and modules and dependencies when your project grows. Space Kablooey fucked around with this message at 03:25 on Apr 1, 2017 |
# ? Apr 1, 2017 03:20 |
|
Malcolm XML posted:This has little to do with python being compiled or not: pandas has a ton of overhead if you don't do things the correct way Huh, I was expecting the pandas overhead to be much smaller. Some testing shows that it goes: vectorized numpy > vectorized pandas >> iterative numpy >>> iterative pandas An iterative solution in something with known types (C/Fortran/Java/numba/Julia/whatever) will still be faster for complicated calculations than the vectorized versions since the vectorized ones destroy all cache locality once the dataset is big enough (by the time you go back to do operation 2 on item 1, it's been booted out of the cache), but you can still get enough speedup to move some algorithms from unworkable to useable. Python's speed problems mostly don't have to do with it being compiled in advance or not. They're consequences of how it works with types and the stuff it lets you change. It's basically impossible to JIT compile stuff like you would in Java because you can't prove that a loop isn't doing things like monkey patching functions or operators at some random loop iteration in the middle. The interpreter has to execute what you put down naively since it can't prove that it's safe to take any shortcuts. Timing results: code:
code:
|
# ? Apr 1, 2017 05:59 |
|
xpander posted:Echoing the other replies that Flask/Django are perfect for this. On the operational side, check out Zappa for some sweet serverless action. This will let you use some AWS services(API Gateway, Lambda) to run the application code without having to keep a server online. You'll fall well within the free tier for development usage, and probably well into their production load as well. How do you trigger local raspberry pi functions with Lambda?
|
# ? Apr 1, 2017 06:15 |
|
Tigren posted:How do you trigger local raspberry pi functions with Lambda? It's a unicornhat Python library so I can just call the function that I already wrote to light the UH when the webhook gets a hit, I think
|
# ? Apr 1, 2017 06:38 |
|
Foxfire_ posted:Huh, I was expecting the pandas overhead to be much smaller. Some testing shows that it goes: Have you tried with Cython? (no clue if it works with pandas or numpy)
|
# ? Apr 1, 2017 14:08 |
|
Foxfire_ posted:Huh, I was expecting the pandas overhead to be much smaller. Some testing shows that it goes: It is entirety possible to JIT compile python using techniques similar to JavaScript -- hidden classes, partial evaluation, polymorphic inline caching-- but there's a lot more money behind Js dev Pypy does a lot of good things but is a research project first . Maybe graal will help , jruby augmented with truffle and graal is insanely fast comparatively
|
# ? Apr 1, 2017 14:49 |
|
Is there anything like the JVM spec but for Python? I want to know what I can assume about the way Python works. I want to know what's the memory overhead for creating an object, for example. How many bits are in an integer? And so on. I don't want to cobble together answers from StackOverflow that maybe describe the behavior of a specific Python implementation that isn't necessarily fixed, for example. Also, I'd like to separately (so that I know what is prescribed for all Python implementations, and what is specific to or just an implementation detail of the most popular implementation) about the internals of the most popular Python implementation. I'm guessing that's CPython, but I imagine 2.7 and 3.blah are pretty different. In general, though, I want to know stuff like how does garbage collection work in CPython? What happens when I create a thread? How do locks work? Java has a bunch of explanation about "happens-before" relationships when writing multi-threaded programs - what's the Python equivalent?
|
# ? Apr 1, 2017 20:01 |
|
Ugh. From the Python documentation, "Floating point numbers are usually implemented using double in C;": https://docs.python.org/2/library/stdtypes.html#numeric-types-int-float-long-complex "Usually". So what's the behavior I can assume I always have?
|
# ? Apr 1, 2017 20:16 |
oliveoil posted:Is there anything like the JVM spec but for Python? CPython is the standard. The best place to start is probably the Python/C API Reference Manual Garbage Collection is handled by reference counting. Objects whose live references hit 0 are immediately. Cpython countains an occasional cyclic garbage detector; while the running of the garbage collection can be forced, outside of that it's behavior is not (meant to be) predictable. Python is more about knowing how things behave than 'what they are'. EG, All integers are implemented as “long” integer objects of arbitrary size. Python Doubles behave as IEEE 64-bit doubles, full stop. If you want to know more about Python's data model and how things work behind the scenes, I recommend Fluent Python, as I do to everyone.
|
|
# ? Apr 1, 2017 20:30 |
|
oliveoil posted:Is there anything like the JVM spec but for Python? LMAO its whatever Guido fever dreams up and shits out in Cpython Hell it took a heroic effort to get ruby specified and that had people who cared behind it Spelunk through the source. Cpython uses a ref counting mechanic fwiw
|
# ? Apr 1, 2017 20:50 |
|
I'm still trying to make this scraper work. I've abandoned BeautifulSoup and totally committed to Scrapy, my spider works but I can't make it pull the exact pieces I need. I'm using this code as my guide, but it's not fully working: https://github.com/jayfeng1/Craigslist-Pricing-Project/blob/master/craigslist/spiders/CraigSpyder.py He explains the methodology here: http://www.racketracer.com/2015/01/29/practical-scraping-using-scrapy/ Python code:
I run this command to execute the program: scrapy crawl basic -o items.csv -t csv. If remove the second method I can get a spreadsheet with titles and links, but I need the geotag. Any ideas?
|
# ? Apr 3, 2017 20:28 |
|
FingersMaloy posted:I'm still trying to make this scraper work. I've abandoned BeautifulSoup and totally committed to Scrapy, my spider works but I can't make it pull the exact pieces I need. I'm using this code as my guide, but it's not fully working: The error message is telling you that you are trying to concatenate a string and a list - apparently item['link'] is a list. I think your error is your for statement ("for titleS in titles:"), where I think you actually want to say "for title in titleS:", and then change occurrences of titleS in the for loop to title.
|
# ? Apr 3, 2017 20:49 |
|
I changed the first titles to title and edited the loop but it's throwing the same error.
|
# ? Apr 4, 2017 02:06 |
|
The error is pretty straightforward, item['link'] is a list of somethings (or it could be empty). You can try printing it out and seeing if whatever you want is in there, and then you can concatenate with your follow link.
|
# ? Apr 4, 2017 02:54 |
|
|
# ? Apr 23, 2024 11:40 |
|
From a quick look at the documentation extract() returns a list of things that match your selector, and extract_first() pulls out a single element You're probably getting lists with a single item when you really just wanted the item on its own
|
# ? Apr 4, 2017 09:12 |