Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Japanese characters in Flask
#1
Hi!
I want to iterate through this dictionary in Flask:

hiragana_dict = {"a": u"\u3042", "i": u"\u3044", "u": u"\u3046", "e": u"\u3048", "o": u"\u304A", 
"ka": u"\u304B", "ki": u"\u304D", "ku": u"\u304F", "ke": u"\u3051", "ko": u"\u3053",
"sa": u"\u3055", "shi": u"\u3057", "su": u"\u3059", "se": u"\u305B", "so": u"\u305D",
"ta": u"\u305F", "chi": u"\u3061", "tsu": u"\u3064", "te": u"\u3066", "to": u"\u3068",
"na": u"\u306A", "ni": u"\u306B", "nu": u"\u306C", "ne": u"\u306D", "no": u"\u306E",
"ha": u"\u306F", "hi": u"\u3072", "fu": u"\u3075", "he": u"\u3078", "ho": u"\u307B",
"ma": u"\u307E", "mi": u"\u307F", "mu": u"\u3080", "me": u"\u3081", "mo": u"\u3082",
"ya": u"\u3084", "yu": u"\u3086", "yo": u"\u3088",
"ra": u"\u3089", "ri": u"\u308A", "ru": u"\u308B", "re": u"\u308C", "ro": u"\u308D",
"wa": u"\u308F", "wo": u"\u3092", "n": u"\u3093"}
The loop works just fine in Python script in IDE, but when I try to use it in Flask, I get SyntaxError:
Error:
File "C:\Users\Ewa\Desktop\KanaSite\kana\kana_app.py", line 28 hiragana_dict = {"a": u"\u3042", "i": u"\u3044", "u": u"\u3046", "e": u"\u3048", "o": u"\u304A", ^ SyntaxError: invalid syntax
(line 28 is just where the dictionary starts)
I tried adding # -*- coding: utf-8 -*- comment, importing unicodedata and using Japanese characters instead of code points (found those solutions somewhere on the internet), but nothing worked.

How can I display Japanese characters on the website using Flask? Thanks in advance!
Reply
#2
there is no problem with the dict.
It's probably something on the previous line.
Please post your full [Flask] code (minimal reproducible example to demonstrate your problem).
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
It works. It must be something before line 28.

In [5]: pprint.pprint(hiragana_dict)
{'a': 'あ',
 'chi': 'ち',
 'e': 'え',
 'fu': 'ふ',
 'ha': 'は',
 'he': 'へ',
 'hi': 'ひ',
 'ho': 'ほ',
 'i': 'い',
 'ka': 'か',
 'ke': 'け',
 'ki': 'き',
 'ko': 'こ',
 'ku': 'く',
 'ma': 'ま',
 'me': 'め',
 'mi': 'み',
 'mo': 'も',
 'mu': 'む',
 'n': 'ん',
 'na': 'な',
 'ne': 'ね',
 'ni': 'に',
 'no': 'の',
 'nu': 'ぬ',
 'o': 'お',
 'ra': 'ら',
 're': 'れ',
 'ri': 'り',
 'ro': 'ろ',
 'ru': 'る',
 'sa': 'さ',
 'se': 'せ',
 'shi': 'し',
 'so': 'そ',
 'su': 'す',
 'ta': 'た',
 'te': 'て',
 'to': 'と',
 'tsu': 'つ',
 'u': 'う',
 'wa': 'わ',
 'wo': 'を',
 'ya': 'や',
 'yo': 'よ',
 'yu': 'ゆ'}
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#4
It's my first time using Flask and the first project like that in general, so it's highly possible I've made mistake somewhere else.

Here's a route:

@app.route("/hiragana_open", methods=['GET', 'POST'])

hiragana_dict = {"a": u"\u3042", "i": u"\u3044", "u": u"\u3046", "e": u"\u3048", "o": u"\u304A", 
"ka": u"\u304B", "ki": u"\u304D", "ku": u"\u304F", "ke": u"\u3051", "ko": u"\u3053",
"sa": u"\u3055", "shi": u"\u3057", "su": u"\u3059", "se": u"\u305B", "so": u"\u305D",
"ta": u"\u305F", "chi": u"\u3061", "tsu": u"\u3064", "te": u"\u3066", "to": u"\u3068",
"na": u"\u306A", "ni": u"\u306B", "nu": u"\u306C", "ne": u"\u306D", "no": u"\u306E",
"ha": u"\u306F", "hi": u"\u3072", "fu": u"\u3075", "he": u"\u3078", "ho": u"\u307B",
"ma": u"\u307E", "mi": u"\u307F", "mu": u"\u3080", "me": u"\u3081", "mo": u"\u3082",
"ya": u"\u3084", "yu": u"\u3086", "yo": u"\u3088",
"ra": u"\u3089", "ri": u"\u308A", "ru": u"\u308B", "re": u"\u308C", "ro": u"\u308D",
"wa": u"\u308F", "wo": u"\u3092", "n": u"\u3093"}

def hiragana_open():
    score = 0
    romh = random.choice(list(hiragana_dict.keys()))
    kanah = hiragana_dict[romh]

    if request.method = 'post':
        answer = request.form.get('answer')

    return render_template('hiragana_open.html', title="Hiragana")
And template:
          {% for q in range(20) %}
            <p>Question # {{ q+1 }}</p>
            <p lang="ja">{{ kanah }}</p>
            <form action='/' method='post'> <input type='text' id='answer' name='answer'></form>
            {% if {{answer.lower()}} == {{ romh }} %}
              <p>Correct!</p>
              {{ score }} += 2
            {% else %}
              <p>Wrong! Correct answer is {{ romh }}!</p>
              {{ score }} -= 1
            {% endif %}
              <p>Your score so far: {{score}}. </p>
          {% endfor %}
I wonder if maybe it's something with form in the template?
Reply
#5
Line 1 contains:
@app.route("/hiragana_open", methods=['GET', 'POST'])
Which should be the decorator for the function above hiragana_open.
Put the decorator in line 13.

@decorator("/path")
def some_func():
    return ""

# is the same like

def some_func():
    return ""


some_func = decorator("/path")(some_func)
Accidentally you did it with the dict.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020