Python Forum

Full Version: Translator doens't show in my web
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am currently working on my website, which is a translator which you input a phrase and it gets translated into an invented language.

Here's the code of the translator function:

def translator(phrase):
    translation = ""
    for letter in phrase:
        if letter.lower() in "a":
            if letter.isupper:
                translation = translation + "U"
            else:
                translation = translation + "u"
        elif letter.lower() in "t":
            if letter.isupper:
                translation = translation + "A"
            else:
                translation = translation + "a"
        elif letter.lower() in "c":
            if letter.isupper:
                translation = translation + "G"
            else:
                translation = translation + "g"
        elif letter.lower() in "g":
            if letter.isupper:
                translation = translation + "C"
            else:
                translation = translation + "c"
    return translation
However, I am stuck in showing this funcion in my web, here's the code in views.py:

from .translate import translator

def translator_view(request):
    
    return render(request,'main/translator.html')


def translated_view(request):
    text = request.GET.get('text')
    print('text:', text)
    translate = translator
    dt = translator.detect(text)
    tr = translated.text
    context = {
        'translated': tr

    }
    return render(request, context, 'main/translated.html')
Here's the template where you introduce the text:
<form action="{% url 'translated' %}" method= "get">
    <div class="form-group">
        <center><h2 class = "display-3">TRANSLATE YOUR DNA CHAIN</h2></center>
        <br>
        <br>
        <textarea class="form-control" id="exampleFormControlTextarea1" rows="6"></textarea>
        <br>
        <button type='Submit' class= "btn btn-primary btn-lg btn-block">Translate</button>


    
      </div>   
</form>
Here's the template that should would show the translation.

{% extends "base.html"%}

{% block content%}

<div >
    <center><h2 class = "display-4">DNA TRANSLATED SUCCESFULLY INTO</h2></center>
    <br>
    <br>
    <br>

    <h3>
        {{ translated }}
    </h3>
       

    
</div>   

{% endblock content%}
Take a look at str.maketrans and str.translate methods; These methods allow you to avoid sophisticated if/else if
constructions:

translation_table = dict()
translation_table.update(str.maketrans('atcg', 'uagc'))
translation_table.update(str.maketrans('atcg'.upper(), 'uagc'.upper()))
in this case translator could be rewritten as follows:

def tranlator(phrase):
    return phrase.translate(translation_table)
Finally, I don't understand translator.detect, translator.text, are these methods? where were they defined?


def translated_view(request):
    text = request.GET.get('text')   # you url should include `text` variable, e.g. http://yourdomain.com/?text='ccttaagg'
    print('text:', text)  # Does this print something to console or not?
    context = {
        'translated': translator(text)
    }
    return render(request, context, 'main/translated.html')