Flask & the Fetch API (AJAX?) - Python on the web - Learning Flask Series Pt. 10

Поделиться
HTML-код
  • Опубликовано: 10 сен 2024
  • In this episode, you'll learn how to use the Fetch API to make asynchronous requests from the client to your Flask app. An upgrade to the AJAX JavaScript API.
    Enjoying this type of content? Head over to my website at pythonise.com for full length text based tutorials, courses and guides.
    Want to help me make even better content? You can by supporting me on Patreon! / juliannash

Комментарии • 74

  • @MatSzub
    @MatSzub Год назад +3

    I was looking for a clear answer how to perform communication between front to back-end for a long time. And after that I found this tutorial, which is clear and understandable for me as a beginner. Julian Nash - thanks a lot. It gives me a milestone in my first programming project.

  • @michaellk2254
    @michaellk2254 3 года назад +8

    you have NO IDEA, how MUCH OF A HEADACHE YOU SAVED ME. OH MY GOODNESS. THANK YOU

  • @jonsnow9758
    @jonsnow9758 3 года назад +2

    Best Python Flask Jinja Tutorial available on RUclips !!! He has explained each and every thing properly.
    That's exactly I was looking for... *Amazing* ....Thanks a ton Julian!!!

  • @joaopaulocoimbra9381
    @joaopaulocoimbra9381 2 года назад

    Amazed by all the talents Charlie Cox have.

  • @KirkLHemmett
    @KirkLHemmett 3 года назад +1

    I just completed my project seeing your video. Great help.

  • @brutalbutler
    @brutalbutler 5 лет назад +10

    vids are class mate, wish i found them earlier!

  • @muhammadshafeeq8779
    @muhammadshafeeq8779 3 года назад

    Thank you soo much for taking your time to do this. I dunno if u hear this a lot but ur special.

  • @soldadopreciso
    @soldadopreciso 3 года назад

    Thanks pal, very useful tutorial, greetings from South America. Greetings fro the flask community.

  • @MegaMooj
    @MegaMooj 7 месяцев назад

    This is great content. Thank you!

  • @jitendra2510
    @jitendra2510 4 года назад +1

    Thank you very very very very very.....much.
    You saved project.
    Thank you again.

  • @pro_grammar
    @pro_grammar Год назад

    You really helped me solve a big problem. Bless you

  • @liquathrushbane2003
    @liquathrushbane2003 2 года назад

    Gah! This video got me. Didn't see (in the previous video) about adding a {% block script %}{% blockend %} in the public_template.html so it took ages to get working. Still, enjoying the series so far.

  • @amd.615
    @amd.615 2 года назад

    Very Helpful, thAnks to you 👍👍👍

  • @adomassem
    @adomassem Год назад

    Thanks for great tutorial, really helped me with my project!

  • @nguyenhailong6522
    @nguyenhailong6522 3 года назад

    It hit right the thing I want. Love your video, great content with an example from the server side. I had been struggling with all the documentation which has nothing on the server-side, and I cant understand how and where to send the fetch url, but your video saves me. So thump up for ya !

  • @BharatChawla55
    @BharatChawla55 2 года назад

    Bro you just saved my day! Thanks a ton!

  • @guinea_horn
    @guinea_horn 3 года назад +1

    Do you have this code up anywhere? It's a bit inconvenient to have to scrub through the video to find bits that I'm referencing in my own application.

  • @bull2026
    @bull2026 3 года назад

    Saved my day ..

  • @PeterCorsa
    @PeterCorsa 5 месяцев назад

    Hello Julian, thank you for the help. I was hoping to view the json data in the terminal on the web page, what video is that in?

  • @junivensaavedra882
    @junivensaavedra882 Год назад

    Excellent resource, thank you very much. :)

  • @wduandy
    @wduandy 4 года назад +1

    You're an amazing teacher mate, well done!

  • @yashvarshney6761
    @yashvarshney6761 3 года назад +3

    Tip: Always show code link at the end of the video.

  • @baconmanbro
    @baconmanbro 4 года назад

    Thanks for your help mate! Last few days was wrapping my mind around this topic, and just clicked while watching this video.

  • @gabriellsscolaro
    @gabriellsscolaro Год назад

    How to hide this script from the client side, or can I make a post of these values and sent to the back end to do some stuff in a python program (back-ended)

  • @sagnikchakraborty4750
    @sagnikchakraborty4750 4 года назад

    This guy saved me !!! Thank A Lot !!!

  • @jonsnow9758
    @jonsnow9758 3 года назад

    *Need HELP* As per this tutorial I'm sending data with POST request using fetch();
    in my views.py file I'm able to print it in the console. But when I pass it using render_template() to a html file, the table rows which I expect to update are NOT updating.

  • @gamex-animex6289
    @gamex-animex6289 9 месяцев назад

    is it possible to send data from flask back to javscript without using jinja?

  • @manhdannguyen6059
    @manhdannguyen6059 3 года назад

    Thank you very much

  • @krishnapatel6821
    @krishnapatel6821 3 месяца назад

    why use fetch() and not Ajax get or post to retrieve and send data back to server?

  • @mbuzifromthehood
    @mbuzifromthehood 3 года назад

    Love your work manze.

  • @Sclafus
    @Sclafus 3 года назад

    Thanks for this amazing tutorial!

  • @stephenthumb2912
    @stephenthumb2912 2 года назад

    At its basics, it's just a way to perform API POST, GET..., directly from vanilla JS inside the DOM, then you're just communicating with your python backend as normal. For some reason that was confusing at the start.

  • @christophedupin8130
    @christophedupin8130 4 года назад

    i m getting an error : Fetch error: TypeError: Failed to fetch
    any idea?
    how could the example work :
    response.json().then(function (data)
    response.json() object doesnt have any methode call then and it s note a promise

  • @EnglishRain
    @EnglishRain 3 года назад

    Amazing, thank you!

  • @ryanhinchliffe7771
    @ryanhinchliffe7771 4 года назад

    Thanks, for making this Video Julian :)

  • @Blocks0
    @Blocks0 4 года назад

    Great tutorial. Thanks!

  • @TheCacoVenom
    @TheCacoVenom Год назад

    good explanation but I really want to click and move another route by rendering a template, so far I am in the route but since is not JSON and instead is

  • @scottcurry3767
    @scottcurry3767 4 года назад

    Awesome stuff man, this helped me a lot!

  • @annakbballin7
    @annakbballin7 3 года назад

    Is there a reason an else statement is not included in the .then method?

  • @alharethturab7612
    @alharethturab7612 3 года назад

    is it possible to send octet-stream inside the json along with other types of data?

  • @danielandresclavijogiraldo4953
    @danielandresclavijogiraldo4953 3 года назад

    If you use Token CSRF, I recommend putting this on the headers "X-CSRF-TOKEN": csrf
    Si utiliza Token Csrf, recomiendo poner esto en las cabeceras "X-CSRF-TOKEN": csrf
    Example/Ejemplo:
    csrf = 'Your token'
    headers: new Headers({
    "Content-Type": "application/json",
    "X-CSRF-TOKEN": csrf
    })

  • @Imufur
    @Imufur 3 года назад

    why use this and not a request.args or request.form or something like that?

  • @PatHess
    @PatHess 4 года назад

    very helpful....I'll check out some more of your videos....I did spend some time running down my typos..nothing new there

  • @adriangonzalez346
    @adriangonzalez346 4 года назад

    The best!!

  • @boriskrasilnikovs4717
    @boriskrasilnikovs4717 3 года назад

    thank you merry much!

  • @m3wt518
    @m3wt518 5 лет назад +1

    Hi! I'm getting this error in the console guestbook:72 Uncaught ReferenceError: submit_entry is not defined
    at HTMLButtonElement.onclick (guestbook:72). Could you help please?

    • @m3wt518
      @m3wt518 5 лет назад

      oh nevermind the question. I figured it out. Thanks for this tutorial! Very helpful

    • @P-2501
      @P-2501 Год назад

      @@m3wt518 how did you figure it out? I'm stuck with that issue. Thanks.

    • @P-2501
      @P-2501 Год назад

      @@m3wt518 Nevermind. I´ve already fixed it. :D

  • @ik4rus2k
    @ik4rus2k 2 года назад

    Hey Julian, thanks for the great video! Could you please explain how to actually return the response? i always get "Undefined"

  • @vavkus
    @vavkus 5 лет назад

    thanks, helpfull example!.

  • @saurabhagarwal9253
    @saurabhagarwal9253 3 года назад

    very useful video.. few things need to be updated in the code to make it work error-free

  • @eugeniogonzato
    @eugeniogonzato 5 лет назад

    Helpful, next time will be very good if we will meke a zoom that permits to read you code watching the video from a smart phone

  • @sayhellotoroy
    @sayhellotoroy Год назад

    Flask forms would work better in this case I believe

  • @unclegood
    @unclegood 4 года назад

    Thanks for the video!
    A question about ${window.origin}. What programming language is that?
    It does not seem to work for me, I use the below instead.
    fetch(window.location.href+'getjson',{
    Do you know if there is any difference?

    • @maxgruntgens9000
      @maxgruntgens9000 3 года назад +2

      It's JavaScript and a more modern feature called string interpolation by Template Literals. ${variable} inside backticks will be read out, computed, and put into the resulting string, Example: const name = "yichuan"; console.log(`My name is ${name}`); // Prints "My name is yichuan". See developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals
      You are using "old style" string concatination syntax.

  • @sonny8595
    @sonny8595 4 года назад

    What would be the reason to use Fetch API, instead of form data as in Pt 7? (Or maybe requests if that is possible?)
    Thanks! I like your videos a lot, very explanatory!
    I also watched the series of Corey Shafer on Flask (which I liked BTW), but yours are more instructive and clearer so far. You use less additional libraries, which let it be more Pythonic.

    • @Tntpker
      @Tntpker 2 года назад +1

      The advantage of async/fetch/asyncio is that it is non-blocking. Other parts of your code can still be executed when awaiting. Some applications I found fetch to be useful for include polling and checking form content prior to submitting.
      Checking form content:
      What you may want to do is to check that the form content is correct before it is actually submitted. For example, if your user can input a file with only certain type(s) of extension(s), you will want to check if the provided file extension is correct before submitting to the server. If it is not, you give feedback to the user. In JS preventDefault() can be used for this, followed by fetch() after having made sure the form data is correct. A double check on the server side should be done as well (in the file extension example you could use Python's assert keyword)
      Polling:
      If you're doing some computationally expensive task in the background, you may want to use fetch to want to periodically check when the task is completed. You could use Celery to offload an expensive task, and use fetch to poll the server every couple of seconds to check if the task is completed. If you don't do this, your request will time out after a minute or so

  • @baraasammoud9148
    @baraasammoud9148 5 лет назад

    hello julian , really i need you help please , can we use the datatime with flask ?
    can we use the datatime with flask ?
    if yes , please how if we limted two date , means date of start and date of end ?
    :) :)

    • @juliannash3590
      @juliannash3590  5 лет назад

      Of course, you can use any Python library with Flask, including datetime.
      Can you explain more about what you're trying to do?

    • @baraasammoud9148
      @baraasammoud9148 5 лет назад

      @@juliannash3590 good evening , :)
      I have a file contains two columns the first column contains the values (I have 1 and -1) the second column the date and I have to computer the number of 1 and -1 from a date start and date end.
      u can help me ?

    • @vatsalaykhobragade
      @vatsalaykhobragade 5 лет назад

      Yes you can use datetime module for that

  • @coderpreneur
    @coderpreneur 5 лет назад

    awesome video buddy, keep it up ;)

  • @fulstak
    @fulstak 5 лет назад

    Getting an error: ReferenceError: submit_entry is not defined

    • @jamesm1858
      @jamesm1858 4 года назад

      If anyone is having this trouble, for me it was because the script block was not being loaded as i was missing {% block script %}{% endblock %} in the template.

  • @Emphraim13
    @Emphraim13 4 года назад

    Thanks you saved my ass

  • @mateoscarafia5110
    @mateoscarafia5110 4 года назад

    coooolll

  • @ivanmclennon
    @ivanmclennon 3 года назад +1

    this was really fun, shame your channel's dead

  • @anarabiyev9766
    @anarabiyev9766 3 года назад

    Videos and explanation are great, but I feel like the videos go from "Flask Tutorial" to "HTML tutorial". I fully get that they are related, but I would be happier I you mainly talked about the basics and main concepts of flask without going into coding of HTML.

  • @zsadezbuchanon2808
    @zsadezbuchanon2808 3 года назад

    Excellent stuff man. Thank you so much.