cheat.sh
cheat.sh copied to clipboard
python and django answers
Hi,
I think the support for python and especially the answers can be improved. For example it does not give any answers about django or flask, popular python web frameworks. Similarly, when asked about python/pandas or python/pandas/read_csv instead of giving the docs or similar, it seemingly gives random functions or stackoverflow answers (at least it feels like it)
Hi @step21,
yes, I totally agree with you, the quality of the answers can be improved, though I can't agree that the service does not deliver any answers about django, flask etc.
I've just checked it with a couple of questions that came on my mind, and the answers were pretty reasonable in my opinion:
$ curl cht.sh/python/flask+server+port
# How do I get Flask to run on port 80?
#
# 1- Stop other applications that are using port 80.
# 2- run application with port 80 :
if __name__ == '__main__':
app.run(host='0.0.0.0', port=80)
# [Amir Mofakhar] [so/q/20212894] [cc by-sa 3.0]
$ curl cht.sh/python/flask+json+response
# Return JSON response from Flask view
#
# Pass the summary data to the jsonify
# (http://flask.pocoo.org/docs/latest/api/flask.json.jsonify) function,
# which returns a JSON response.
from flask import jsonify
@app.route('/summary')
def summary():
d = make_summary()
return jsonify(d)
# As of Flask 0.11, you can pass any JSON-serializable type, not just
# dict, as the top level object.
#
# [codegeek] [so/q/13081532] [cc by-sa 3.0]
and the same about django:
$ curl cht.sh/python/django+server+port
# django change default runserver port
#
# create a bash script with the following:
#!/bin/bash
exec ./manage.py runserver 0.0.0.0:<your_port>
# save it as runserver in the same dir as manage.py
chmod +x runserver
# and run it as
./runserver
# [Pablo Albornoz] [so/q/23639085] [cc by-sa 3.0]
$ curl cht.sh/python/django+json+response
# Creating a JSON response using Django and Python
#
# I usually use a dictionary, not a list to return JSON content.
import json
from django.http import HttpResponse
response_data = {}
response_data['result'] = 'error'
response_data['message'] = 'Some error message'
# Pre-Django 1.7 you'd return it like this:
return HttpResponse(json.dumps(response_data), content_type="application/json")
# For Django 1.7+, use JsonResponse
# (https://docs.djangoproject.com/en/dev/ref/request-response
# /jsonresponse-objects) as shown in this SO answer
# (https://stackoverflow.com/a/24411716/7376) like so :
from django.http import JsonResponse
return JsonResponse({'foo':'bar'})
# [Tom] [so/q/2428092] [cc by-sa 3.0]
Please keep in mind, that it is (currently) not supposed to provide access to the documentation repositories of the projects. The modular architecture of cheat.sh supports the feature, but the documentation repositories are not yet attached.
As soon as it is implemented, it will be possible to access the function documentation as you suggest, i.e. queries like:
curl cht.sh/go/ref/regexp.FindAllString
curl cht.sh/python/ref/re.findall
curl cht.sh/js/regexp.exec
would work.
Could you please show the questions about Flask and Django that did not work for you? I can say you then, were they supposed to work or not.
I am not sure that I am doing this the right way, but on the vim plugin, for frameworks, I replace the filetype, for instance if I want to know how to do a file upload form in django:
$ curl cht.sh/django/view+form+file
<!--
- Django file upload using Form
-
- well you need to specify the upload path in your models
-->
file = models.FileField(null=True, blank=True,upload_to='files')
<!--
- and make sure you have MEDIA_ROOT and MEDIA_URL defined in your
- settings.py
-
- in your form
-->
<form method="post" action="" enctype="multipart/form-data">
{% csrf_token %}
...
</form>
<!-- [Exprator] [so/q/45727293] [cc by-sa 3.0] -->
I've tried this kind of queries (simple ones) with django and symphony, the answers seems often fair
@dbeniamine David, you are doing it right, the only thing that should be fixed in cheat.sh here, is that the programming language of the answer is assumed wrong for the cases when it is not specified as the part of the query, and that's why the output is rendered with wrong syntax highlighting and wrong comments.
Workaround is specify python as the part of the query:
curl cht.sh/python/django+view+form+file
but actually the bug should be fixed, and it should be possible to used django, flask etc. as the section name
Yes but the answer of django/view+form+file is quite different (and I think more pertinent) from python/django+view+form+file:
$ curl cht.sh/django/view+form+file
<!--
- Django file upload using Form
-
- well you need to specify the upload path in your models
-->
file = models.FileField(null=True, blank=True,upload_to='files')
<!--
- and make sure you have MEDIA_ROOT and MEDIA_URL defined in your
- settings.py
-
- in your form
-->
<form method="post" action="" enctype="multipart/form-data">
{% csrf_token %}
...
</form>
<!-- [Exprator] [so/q/45727293] [cc by-sa 3.0] -->
$ curl cht.sh/python/django+view+form+file
# Is there a way to upload a file from view in django?
#
# So I put in an answer to my original question:
input = "blah file input"
filename = "media/filename.txt"
storage = default_storage.open(filename, 'w+')
storage.write(input)
storage.close()
# [GetItDone] [so/q/14780653] [cc by-sa 3.0]
@dbeniamine Yes, David, actually you are right, the additional word (python) though makes the answer looks pretty (because of the correct comments) make the search not so precise.
I've (experimentally) extended the cheat.sh queries so that for the section names can be used not only programming languages name, but other topics too. I've added several new sections, that are not programming languages:
djangoflaskgitcmake
I have the feeling, that it is just a beginning, and we will add a lot of the new sections in future.
The queries are processed correctly now, and the comments look as they should.
Even more, for the git and cmake sections the :learn queries work:
~$ curl cht.sh/django/view+form+file
# Django file upload using Form
#
# well you need to specify the upload path in your models
file = models.FileField(null=True, blank=True,upload_to='files')
# and make sure you have MEDIA_ROOT and MEDIA_URL defined in your
# settings.py
#
# in your form
<form method="post" action="" enctype="multipart/form-data">
{% csrf_token %}
...
</form>
# [Exprator] [so/q/45727293] [cc by-sa 3.0]
~$ curl cht.sh/flask/json+response
# Return JSON response from Flask view
#
# Pass the summary data to the jsonify
# (http://flask.pocoo.org/docs/latest/api/flask.json.jsonify) function,
# which returns a JSON response.
from flask import jsonify
@app.route('/summary')
def summary():
d = make_summary()
return jsonify(d)
# As of Flask 0.11, you can pass any JSON-serializable type, not just
# dict, as the top level object.
#
# [codegeek] [so/q/13081532] [cc by-sa 3.0]
$ curl cht.sh/cmake/:learn | less -R
$ curl cht.sh/git/:learn | less -R
I don't know, how we could specify while using the vim plugin, that now a django or flask query should be done, and thus not /python/flask+json+response would be sent, but flask/json+response,
but probably it is not so easy.
Maybe the queries containing both python and djano should be rewritten on the server side:
python/flask+json+response => flask/json+response
then nothing on the plugin side should be changed.